如何在字符串中显示我从(服务器)接收的图像?
我正在使用AsyncTask
以下是代码:
的AsyncTask
public class sendRequest extends AsyncTask<String, Void,String> {
String response="";
Context context;
sendRequest(Context context)
{
this.context=context;
// this.list=list;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params)
{
URL url;
BufferedReader reader = null;
String link =<i> "example.com/example.php";</i>
try {
url = new URL(link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5 * 1000);
connection.setReadTimeout(5 * 1000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.connect();
String inputToFile = URLEncoder.encode("mName", "UTF-8") + "=" + URLEncoder.encode("name", "UTF-8");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(inputToFile);
writer.flush();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
//Here i have add data to string how to display images
//The result of Toast in onpostexecute is
//{"images":[{"image":"http:\/\/example.com\/example.jpg"},{"image":"http:\/\/example.com\/example.jpg"}]}
response += line;
}
//Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
//Toast.makeText(context,bmp.toString(),Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
// DonorList.setText("Url Error");
e.printStackTrace();
response = "Exception: " + e.getMessage();
} catch (IOException e) {
// DonorList.setText("Connection could not be opened");
e.printStackTrace();
response += "Exception: " + e.getMessage();
}
return response;
}// DO IN BACKGROUND
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
Toast.makeText(context, "Recieved: "+response, Toast.LENGTH_LONG).show();
答案 0 :(得分:0)
使用Glide库在ImageView中加载图像
Glide.with(context).load(image_url).into(imageView);