Hi i am using the AsyncTask
in order to get data from a php files But when i run the application i get this error !
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();
}
}
and here is the logcat
03-13 08:12:05.111: E/AndroidRuntime(1232): FATAL EXCEPTION: AsyncTask #1 03
-13 08:12:05.111: E/AndroidRuntime(1232): java.lang.RuntimeException: An error occured while executing doInBackground()
03-13 08:12:05.111: E/AndroidRuntime(1232): at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-13 08:12:05.111: E/AndroidRuntime(1232): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-13 08:12:05.111: E/AndroidRuntime(1232): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-13 08:12:05.111: E/AndroidRuntime(1232): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-13 08:12:05.111: E/AndroidRuntime(1232): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-13 08:12:05.111: E/AndroidRuntime(1232): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-13 08:12:05.111: E/AndroidRuntime(1232): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-13 08:12:05.111: E/AndroidRuntime(1232): at java.lang.Thread.run(Thread.java:856)
03-13 08:12:05.111: E/AndroidRuntime(1232): Caused by: java.lang.NullPointerException
03-13 08:12:05.111: E/AndroidRuntime(1232): at mr.zereshk.online.Messages_online$RequestPostsTask.doInBackground(Messages_online.java:80)
03-13 08:12:05.111: E/AndroidRuntime(1232): at mr.zereshk.online.Messages_online$RequestPostsTask.doInBackground(Messages_online.java:1)
03-13 08:12:05.111: E/AndroidRuntime(1232): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-13 08:12:05.111: E/AndroidRuntime(1232): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-13 08:12:05.111: E/AndroidRuntime(1232): ... 4 more
Any Help ? Thanks in advance!