如何从数据库中获取图像并将位置设置为数据?

时间:2017-03-08 20:46:01

标签: java android json database

这是我在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应用程序中运行app时,请关闭力量。在logCat中,在ImageDownloaderTask中存在错误

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);

    }

}

1 个答案:

答案 0 :(得分:0)

10.0.3.2替换为10.0.2.2或替换为以192开头的服务器IP地址。