我的Json数组包含两个图像,如何通过url / http下载并在活动中显示

时间:2016-06-21 07:39:28

标签: android json

我的json数组包含两个图像,如何在活动中显示它?那些图像是url的形式。之前和之后是图像视图,我想知道什么是图像下载任务以及如何使用它。

    after = (ImageView) findViewById(R.id.bfrdoogret);
    before = (ImageView) findViewById(R.id.afterdogret);
 }

   class BackGround extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = ProgressDialog.show(Serialno.this, "",
                "Searching. Please wait...", true);
        dialog.setCancelable(true);
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params)
    {
        String values=getvalues();
        return values;
    }



    @Override
    protected void onPostExecute(String s)
    {
        dialog.hide();
        Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
        dis.setText(district);
        city.setText(cit);
        serialno.setText(serial_no);
        gender.setText(gende);

        loction.setText(latitude+  longitude);


    }
}

public String getvalues()
{
    String uri = "http://www.lorryguru.com/domains/dogs/retusingsno.php?district="+District+"&serialno="+SerialNo;
    HttpGet httpGet = new HttpGet(uri);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1)
        {
            stringBuilder.append((char) b);
        }
    }
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    try
    {
        JSONArray arr = new JSONArray(stringBuilder.toString());
        //result = jsonObj.getJSONArray(TAG_RESULT);
        JSONObject c = arr.getJSONObject(0);
        id = c.getString(TAG_ID);
        district = c.getString(TAG_DISTRICT);
        cit = c.getString(TAG_CITY);
        serial_no = c.getString(TAG_SERIALNO);
        gende = c.getString(TAG_GENDER);
        wareno = c.getString(TAG_WARENO);
        befor = c.getString(TAG_BEFORE);
        afte = c.getString(TAG_AFTER);
        latitude = c.getString(TAG_LATITUDE);
        longitude = c.getString(TAG_LONGITUDE);
        return "success";
    }
    catch (JSONException e)
    {
        return "accessproblem";
        //e.printStackTrace();
    }

4 个答案:

答案 0 :(得分:1)

在gradle utm_term

中添加此依赖项
compile 'com.squareup.picasso:picasso:2.5.2'

答案 1 :(得分:0)

使用Picasso从网址异步下载图片。

答案 2 :(得分:0)

你可以使用毕加索:

Picasso.with(this)
       .load(url)
       .into(imageView);

答案 3 :(得分:0)

您必须在现有任务之后添加另一个异步任务,代码如下所示。

 class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap>
{
    private final WeakReference<ImageView> imageViewReference;
    String murl;
    ImageView showView;
    public ImageDownloaderTask(ImageView imageView,String url) {
        imageViewReference = new WeakReference<ImageView>(imageView);
        murl=url;
        showView=imageView;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        final Bitmap bitmap=downloadBitmap(murl);
        if(murl!=null && bitmap!=null)
        {
            addBitmapToMemoryCache(murl, bitmap);
        }
        return bitmap;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {

                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                    //addBitmapToMemoryCache(murl, bitmap);
                }

            }

        }
    }
}

private Bitmap downloadBitmap(String url)
{
    HttpURLConnection urlConnection = null;
    Bitmap bitmap;
    try {
        URL uri = new URL(url);
        urlConnection = (HttpURLConnection) uri.openConnection();
        int statusCode = urlConnection.getResponseCode();
        if (statusCode != HttpStatus.SC_OK)
        {
            return null;
        }
        InputStream inputStream = urlConnection.getInputStream();

        bitmap = BitmapFactory.decodeStream(inputStream);


        return bitmap;
    }

    catch (Exception e)
    {
        urlConnection.disconnect();
        Log.w("ImageDownloader", "Error downloading image from " + url);
    }
    finally
    {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
    return null;
}