毕加索加载图片错误 - Android

时间:2015-01-20 06:18:10

标签: java android json picasso

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

    }

}

执行上面的代码时,我在行Picasso.with(this).load(image1).into(iv7);

上遇到了一些错误

错误是 Picasso类型中带有(context)的方法不适用于参数(MainActivity.LoadProfile)

我编码中的问题是什么。

2 个答案:

答案 0 :(得分:2)

您可以更改

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

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

试一试。

答案 1 :(得分:1)

您已将此行放在Picasso.with(this).load(image1).into(iv7); AsyncTask的onPostExecute中。所以,这里这个指的是AsyncTask而不是Activity的上下文。你必须这样做

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