有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将字符串传递给Uri。下载中的parse()

我通过解析json获得url字符串。当我将这个字符串传递给Uri.parse()方法以使用下载管理器获取文件时,它不起作用。我在发送到Uri之前检查下载URL。parse()并且它有一个正确的值

这是我的代码:

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    holder.txtTitle.setText(questionha.get(position).getQuestionTitle());
    holder.txtDesc.setText(questionha.get(position).getQuestionDesc());
    holder.txtCntDown.setText(questionha.get(position).getQuestionDownCnt());
    holder.txtAuthorName.setText(questionha.get(position).getQuestionAuthorName());
    Glide.with(holder.itemView.getContext()).load(questionha.get(position).getQuestionAuthorPic()).into(holder.imgAuthorPic);
    holder.txtDate.setText(questionha.get(position).getQuestionDate());
    holder.btnDownload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //===========================================================================
            JsonObjectRequest volleyRequest = new JsonObjectRequest(questionha.get(position).getQuestionDownLink(), new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if (response != null || response.length() > 0) {
                        try {
                          ===>  String downloadUrl = response.getString("link");

                            DownloadManager downloadManager = (DownloadManager) context.getSystemService (Context.DOWNLOAD_SERVICE);
                            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl));
                            request.setTitle(questionha.get(position).getQuestionTitle())
                                    .setDescription("در حال دانلود")
                                    .setDestinationInExternalFilesDir(context, "", "salam");
                            long enqueueId = downloadManager.enqueue(request);

                            Log.i("DonwloadUrl",response.getString("link"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });
            AppController.getInstance().addToRequestQueue(volleyRequest);

            //==============================================================================
            //Toast.makeText(context, DownloadUrl, Toast.LENGTH_SHORT).show();


        }
    });

}

当我静态地传递url字符串时,它就工作了!。 这是我的下载URL值:

Screenshot link

谢谢


共 (0) 个答案