2017-07-25 17 views
-8

我怎樣才能顯示圖像,我從(服務器)在字符串中接收? 我使用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

請簡單告訴我們您的問題。只有代碼不會幫助?你想達到什麼目的? – Ashwani

+0

我想在服務器接收的圖像視圖中顯示圖像。 –

回答

0

使用滑翔庫中的ImageView加載圖像

Glide.with(context).load(image_url).into(imageView); 
相關問題