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);