开发者社区> 问答> 正文

Android 链接服务器获取json数据,解析头像url后。

Android 刚进入页面,链接服务器获取json数据,解析图片url,然后联网加载头像图片的操作得不到头像。代码如下。向各位请教。新手,不知道哪里出问题了。

JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST, url, null, new Listener() {
        @Override
        public void onResponse(JSONObject response) {

            JSONObject result;
            String stringUrl = null;
            try {
                result = response
                        .getJSONObject("Result");
                acount = result.getString("Account");
                name = result.getString("UserName");
                stringUrl=result.getString("ImagUrl");
                 String imapath="http://192.168.1.1/"+stringUrl;

                            URL imgUrl=new URL(imapath);
                             HttpURLConnection conn = (HttpURLConnection) imgUrl
                                        .openConnection();
                             Toast.makeText(MyInformationActivity.this, "链接成功", Toast.LENGTH_SHORT).show();
                              conn.connect();
                              InputStream is = conn.getInputStream();
                              Bitmap bm = BitmapFactory.decodeStream(is);

                              ivHeadImage.setImageBitmap(bm);
                              is.close();
                        } catch (Exception e) {
                            Toast.makeText(MyInformationActivity.this, "头像获取异常", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }

                    }
                  };
            } 
            tvNickName.setText(name);

        }


    }, new ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {


        }
    });
    queue.add(request);

展开
收起
爵霸 2016-06-12 13:30:35 2257 0
1 条回答
写回答
取消 提交回答
  • 估计你ajax等获取的图片URL地址不正确,导致后面显示不对。你先要确保你的服务器返回了正确的URL等信息

    2019-07-17 19:33:45
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载