Php 在AsyncTask中执行doInBackground()时出错
嗨,我正在使用Php 在AsyncTask中执行doInBackground()时出错,php,android,android-asynctask,Php,Android,Android Asynctask,嗨,我正在使用AsyncTask从php文件中获取数据,但是当我运行应用程序时,我得到了这个错误 List<Online_Messages_item> o; public class RequestPostsTask extends AsyncTask<Void, Void, Boolean> { ProgressDialog pdLoading; @Override protected void onPreExecut
AsyncTask
从php文件中获取数据,但是当我运行应用程序时,我得到了这个错误
List<Online_Messages_item> o;
public class RequestPostsTask extends AsyncTask<Void, Void, Boolean> {
ProgressDialog pdLoading;
@Override
protected void onPreExecute() {
pdLoading = new ProgressDialog(Messages_online.this);
pdLoading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pdLoading.setMessage("please wait ...");
pdLoading.setIndeterminate(false);
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected Boolean doInBackground(Void... params) {
boolean status = false;
HttpURLConnection urlConnection = null;
try {
URL url = new URL("my url");
urlConnection = (HttpURLConnection) url.openConnection();
Charset charset = Charset.forName("UTF8");
int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), charset));
if (in != null) {
StringBuilder strBuilder = new StringBuilder();
int ch = 0;
while ((ch = in.read()) != -1)
strBuilder.append((char) ch);
String response = strBuilder.toString();
Log.d("Server response:", response);
JSONObject jObject = new JSONObject(response);
JSONArray jsonArray = jObject.getJSONArray("posts");
for(int i=0; i<jsonArray.length(); i++){
Online_Messages_item omi = new Online_Messages_item();
omi.setId(jsonArray.getJSONObject(i).getLong("ID"));
omi.settext(jsonArray.getJSONObject(i).getString("Text"));
omi.setlikes(jsonArray.getJSONObject(i).getString("Likes"));
omi.setpt(jsonArray.getJSONObject(i).getString("PublishTime"));
o.add(omi);
}
if(o.size() >= 1)
status = true;
}
in.close();
}
}
catch(MalformedURLException e){
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
return status;
}
@Override
protected void onPostExecute(Boolean result) {
BaseAdapter ba=new Messages_online_adapter(Messages_online.this,o);
listView.setAdapter(ba);
pdLoading.dismiss();
}
}
有什么帮助吗?提前谢谢 列表o;
List<Online_Messages_item> o;
您从不初始化o
你应该把
o=newarraylist()放在某个地方代码>。我建议使用构造函数Mahfa正如blackbelt建议的那样,您需要向RequestPostsTask类添加一个构造函数。只需向类中添加以下代码行,就可以了:)
public RequestPostsTask(){o=new ArrayList();}
第80行是什么Messages\u online.java
?@Raghunandan这是一项活动第80行是什么。您能在代码中指出行号80吗?stacktrace指向doInBackground。我查了一下那里的“代码”@Raghunandan@blackbelt不可否认。
List<Online_Messages_item> o;
public RequestPostsTask(){ o = new ArrayList<Online_Messages_item>(); }