使用异步 http 响应处理程序从服务器获取图像并设置到图像视图中
get image from server and set into imageview using async http response handler
我的问题是如何使用异步 Http 响应处理程序从服务器获取图像并设置到图像视图中。图像的 JsonObject 是 "profile_image",需要从 Rest api 中获取并显示到我的 ImageView 中。
我的代码:
AsyncHttpClient client = new AsyncHttpClient();
//private ProgressDialog Dialog = new ProgressDialog(post.this);
List<String> userCredentials = UserUtils.getLoginDetails();
client.addHeader("x-user-id", userCredentials.get(0));
client.addHeader("x-auth-token", userCredentials.get(1));
client.get("https://", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
final String response = new String(responseBody);
android.util.Log.e("Response", "" + response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject object = jsonObject.getJSONObject("user");
String attr1 = object.getString("name");
data = "" + attr1;
textView15.setText(data);
if (object.has("profession")) {
String attr2 = object.getString("profession");
data2 = "" + attr2;
textView16.setText(data2);
}
if(object.has("company")){
String attr3 = object.getString("company");
data3 = "" + attr3;
textView38.setText(data3);
}
if(object.has("profile_image")){
}
JSONObject jsonObject1 = object.getJSONObject("emails");
JSONArray jsonArray = jsonObject1.getJSONArray("address");
for (int i = 0; i < jsonArray.length(); i++) {
data += "\n"
+ jsonArray.getJSONObject(i).getString("address")
.toString();
// data += " Name= "+ attr1 +" \n ";
}
textView15.setText(data);
//Dialog.dismiss();
} catch (JSONException e) {e.printStackTrace();}
}
API代码:
"user": {
"profile_image": "https:////image/upload/v1455884587/endflvkyqv3ot37g4unc.png",
"profile_public_id": "endflvkyqv3ot37g4unc"
}
}
使用 Picasso 从服务器加载图像 / URL :
但是,首先检查你的 ImageResponse
在使用 Picasso 之前不要忘记添加 Picasso 库、For Library 和更多关于 Picasso 的信息 see here
if(object.has("profile_image")){
Picasso.with(this)
.load("" + object.getString("profile_image"))
.fit()
.into(imageView);
}
我的问题是如何使用异步 Http 响应处理程序从服务器获取图像并设置到图像视图中。图像的 JsonObject 是 "profile_image",需要从 Rest api 中获取并显示到我的 ImageView 中。
我的代码:
AsyncHttpClient client = new AsyncHttpClient();
//private ProgressDialog Dialog = new ProgressDialog(post.this);
List<String> userCredentials = UserUtils.getLoginDetails();
client.addHeader("x-user-id", userCredentials.get(0));
client.addHeader("x-auth-token", userCredentials.get(1));
client.get("https://", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
final String response = new String(responseBody);
android.util.Log.e("Response", "" + response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject object = jsonObject.getJSONObject("user");
String attr1 = object.getString("name");
data = "" + attr1;
textView15.setText(data);
if (object.has("profession")) {
String attr2 = object.getString("profession");
data2 = "" + attr2;
textView16.setText(data2);
}
if(object.has("company")){
String attr3 = object.getString("company");
data3 = "" + attr3;
textView38.setText(data3);
}
if(object.has("profile_image")){
}
JSONObject jsonObject1 = object.getJSONObject("emails");
JSONArray jsonArray = jsonObject1.getJSONArray("address");
for (int i = 0; i < jsonArray.length(); i++) {
data += "\n"
+ jsonArray.getJSONObject(i).getString("address")
.toString();
// data += " Name= "+ attr1 +" \n ";
}
textView15.setText(data);
//Dialog.dismiss();
} catch (JSONException e) {e.printStackTrace();}
}
API代码:
"user": {
"profile_image": "https:////image/upload/v1455884587/endflvkyqv3ot37g4unc.png",
"profile_public_id": "endflvkyqv3ot37g4unc"
}
}
使用 Picasso 从服务器加载图像 / URL :
但是,首先检查你的 ImageResponse
在使用 Picasso 之前不要忘记添加 Picasso 库、For Library 和更多关于 Picasso 的信息 see here
if(object.has("profile_image")){
Picasso.with(this)
.load("" + object.getString("profile_image"))
.fit()
.into(imageView);
}