毕加索加载图片错误-Android
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();
}
}
}
在执行上面的代码时,我在行 Picasso.with(this).load(image1).into(iv7);
上遇到了一些错误
错误是Picasso 类型中的方法 with(context) 不适用于参数(MainActivity.LoadProfile).
我的编码有什么问题。
您可以更改
Picasso.with(this).load(image1).into(iv7);
至
Picasso.with(MainActivity.this).load(image1).into(iv7);
试一试。
您已将此行 Picasso.with(this).load(image1).into(iv7);
放在 AsyncTask 的 onPostExecute
中。所以,这里 this 指的是 AsyncTask
而不是 Activity
的上下文。你必须这样做
Picasso.with(MainActivity.this).load(image1).into(iv7);
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 类型中的方法 with(context) 不适用于参数(MainActivity.LoadProfile).
我的编码有什么问题。
您可以更改
Picasso.with(this).load(image1).into(iv7);
至
Picasso.with(MainActivity.this).load(image1).into(iv7);
试一试。
您已将此行 Picasso.with(this).load(image1).into(iv7);
放在 AsyncTask 的 onPostExecute
中。所以,这里 this 指的是 AsyncTask
而不是 Activity
的上下文。你必须这样做
Picasso.with(MainActivity.this).load(image1).into(iv7);