0

AsyncTask of EventHome.java

class LoadProfile extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(EventHome.this);
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting Profile JSON
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        String json = null;
        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(PROFILE_URL);
            httppost.setEntity(new UrlEncodedFormEntity(params));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();
            json = EntityUtils.toString(resEntity);

            Log.i("All Events: ", json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return json;
    }

    @Override
    protected void onPostExecute(String json) {
        super.onPostExecute(json);
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        try{
        event_all = new JSONObject(json);
        JSONArray user = event_all.getJSONArray("events");
        JSONObject jb= user.getJSONObject(0);
        String name = jb.getString("name");
        String venue=jb.getString("location");
        String date=jb.getString("date_d");
        String descr=jb.getString("descr");
        String image1=jb.getString("images1");

        // displaying all data in textview

        tv3.setText(name);
        tv4.setText(venue+", "+date);
        tv5.setText(descr);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }

}

json output string

{"events":[{"id":"1","name":"Demo Event","descr":"This is a demo","date_d":"12\/27\/2014","time_t":"","images":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo.png","images1":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo1.png","location":"Ernakulam","publish":"1"},{"id":"2","name":"Flower Show","descr":"xfvslkdfvsw fvgersdgvel fgvmerlkgjer fgertgkjerltgf dfrgergt","date_d":"12\/29\/2014","time_t":"","images":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo.png","images1":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo1.png","location":"Thodupuzha","publish":"1"},{"id":"3","name":"Cultural Event","descr":"sfsdnfkjn sdfrelegkrg fdgvlkgvjmer fdgbelkrek dferlkgelrk dfgerge","date_d":"12\/30\/2014","time_t":"","images":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo.png","images1":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo1.png","location":"Idukki","publish":"1"}]}

I want to display the images from the json values. How can i do that.

4
  • use Picasso for loading those images in ImageView Commented Jan 19, 2015 at 11:10
  • Picasso.with(this).load(image1).into(iv7); //the method with(context)in type picasso is not applicable for the arguments (EventHome.LoadProfile) Commented Jan 20, 2015 at 6:09
  • what is EventHome ? is it Activity name Commented Jan 20, 2015 at 6:17
  • check my answer tell me if you faced any problem Commented Jan 20, 2015 at 6:28

2 Answers 2

1

Try to load image using picasso like this

@Override
protected void onPostExecute(String json) {
   super.onPostExecute(json);
   // dismiss the dialog after getting all products
   pDialog.dismiss();
   try{
        event_all = new JSONObject(json);
        JSONArray user = event_all.getJSONArray("events");
        JSONObject jb= user.getJSONObject(0);
        String name = jb.getString("name");
        String venue=jb.getString("location");
        String date=jb.getString("date_d");
        String descr=jb.getString("descr");
        String image1=jb.getString("images1");

        // displaying all data in textview

        tv3.setText(name);
        tv4.setText(venue+", "+date);
        tv5.setText(descr);
        if(image1 != null && !image1.equalsIgnoreCase(""))
              Picasso.with(EventHome.this).load(image1).into(iv7);
     }catch(Exception e)
     {
         e.printStackTrace();
     }

}

Place picasso jar in libs folder of your project or add it to your dependencies in your build.gradle file (see the Picasso site)

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

protected void onPostExecute(String json) { .....

ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(fallback)
.showImageOnFail(fallback)
.showImageOnLoading(fallback).build();

//initialize image view
ImageView imageView = (ImageView) findViewById(R.id.imageView1)

//download and display image from url
imageLoader.displayImage(image1, imageView, options);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.