Search code examples
javaandroidjsonpicasso

Picasso loading image error-Android


MainActivity.java

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

    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");
        image1=jb.getString("images1");

        // displaying all data in textview

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

    }

}

while executing the above code I got some error on line Picasso.with(this).load(image1).into(iv7);

And the error is the method with(context) in the type Picasso is not applicable for the arguments(MainActivity.LoadProfile).

What's the problem in my coding.


Solution

  • You have placed Picasso.with(this).load(image1).into(iv7); this line in AsyncTask's onPostExecute. So, here this refers that AsyncTask not the context of that Activity. You have to do like this

    Picasso.with(MainActivity.this).load(image1).into(iv7);