这是我在 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=";
这是我的 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);
}
}
最佳答案
将 10.0.3.2
替换为 10.0.2.2
或替换为以 192 开头的服务器 IP 地址。
关于java - 如何从数据库获取图像并设置数据的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42681608/