2017-03-08 134 views
1

這是我在vid.java類如何從數據庫獲取圖像並將位置設置爲數據?

public String LOG = "erorMessage"; 
public String url_cat = "http://10.0.3.2/fd/get_cat.php"; 
public String url_video = "http://10.0.3.2/fd/get_data.php?page="; 
public String Url_Video_by_cat = "http://10.0.3.2/fd/get_data_by_cat.php?cat="; 

的URL,這是我ImageDownloaderTask.java類

private class ImageDownloaderTask extends AsyncTask <HashMap<String , Object> , Void , HashMap<String , Object>> { 

    @Override 
    protected HashMap<String, Object> doInBackground(HashMap<String, Object>... params) { 

     InputStream myStream; 

     String imgurl = (String)params[0].get("image_path"); 

     int position = (Integer)params[0].get("position"); 


     try{ 


      URL url = new URL(imgurl); 
      HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 

      connection.setDoInput(true); 
      connection.connect(); 

      myStream = connection.getInputStream(); 

      File cachDirectory = getBaseContext().getCacheDir(); 

      File temp = new File(cachDirectory.getPath() + "/image_" + position + "_" + curent_page + ".png"); 

      FileOutputStream outputStream = new FileOutputStream(temp); 

      Bitmap b = BitmapFactory.decodeStream(myStream); 

      b.compress(Bitmap.CompressFormat.PNG , 100 , outputStream); 

      outputStream.flush(); 

      outputStream.close(); 

      HashMap<String , Object> bitmap = new HashMap<>(); 

      bitmap.put("image" , temp.getPath()); 
      bitmap.put("position" , position); 


      return (bitmap); 



     }catch (Exception e){ 
      Log.i(LOG , "error in ImageDownloaderTask" + e.toString()); 
     } 


     return null; 
    } 

    @Override 
    protected void onPostExecute(HashMap<String, Object> reslut) { 


     String images = (String) reslut.get("image"); 

     int position = (Integer)reslut.get("position"); 

     SimpleAdapter adb = (SimpleAdapter) l.getAdapter(); 

     HashMap<String , Object> hms = (HashMap<String, Object>) adb.getItem(position); 

     hms.put("image" , images); 

     adb.notifyDataSetChanged(); 

    } 
} 

一切都很好,但是當我運行genymotion應用程序給強制關閉。在logcat中有錯誤inImageDownloaderTask

03-08 15:38:06.761 3417-3687/com.haditv.haditv I/erorMessage: error in ImageDownloaderTaskjava.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused) 

,這是我videosmosoe.java類

public class Videos_mosoe { 

    List<HashMap<String, Object>> pars_video_mosoe(String json) { 

     List<HashMap<String, Object>> All_Video_mosoz = new ArrayList<>(); 

     try { 

      JSONObject joj = new JSONObject(json); 
      JSONArray jArr = joj.getJSONArray("video"); 

      for (int i = 0; i < jArr.length(); i++) { 

       HashMap<String, Object> Videos = new HashMap<String, Object>(); 

       JSONObject temp = (JSONObject) jArr.get(i); 

       Videos.put("id", temp.getString("id")); 
       Videos.put("subject", temp.getString("subject")); 
       Videos.put("image", R.drawable.ic_movie_creation_black_36dp); 
       Videos.put("image_path", temp.getString("image")); 
       Videos.put("description", temp.getString("description")); 
       Videos.put("date", temp.getString("date")); 
       Videos.put("cat_id", temp.getString("cat_id")); 

       All_Video_mosoz.add(Videos); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return (All_Video_mosoz); 

    } 

} 

回答

0

10.0.2.2更換10.0.3.2或與192

+0

仍然是Proglem啓動服務器的IP地址替換:無法連接到本地主機/ 127.0.0.1(端口80):連接失敗:ECONNREFUSED(連接被拒絕) – rohollah

+0

我編輯答案 –

相關問題