i try parse json, but i have problem, when i runing my app, i have next error. i try print log’s but it not printed. He can’t read my rss.json? or another probleM?
I am getting FATAL EXCEPTION : AsyncTask #1 this error in the log cat
I am able to run the code on AVD 4.1.2
Logcat
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:64)
at java.io.InputStreamReader.<init>(InputStreamReader.java:79)
at com.gazetaimage.MainActivity$backTask.doInBackground(MainActivity.java:112)
at com.gazetaimage.MainActivity$backTask.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
AsyncTask Code
class backTask extends AsyncTask<String, Void, JSONObject>{
InputStream ips;
String jstring = null;
JSONObject jsonObj = null;
MainActivity activity;
@Override
protected JSONObject doInBackground(String... params) {
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://mysite.com/RSS/gazeta.js");
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
ips = entity.getContent();
Log.d("Log", ips.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
//MainActivity.java:112 ERROR --->
BufferedReader bufff = new BufferedReader(new InputStreamReader(ips, "UTF-8"));
Log.d("Log", bufff.toString());
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = bufff.readLine()) != null){
sb.append(line + "\n");
}
ips.close();
jstring = sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
jsonObj = new JSONObject(jstring);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObj;
}
protected void onPostExecute(JSONObject result){
super.onPostExecute(result);
}
public void link(MainActivity mainActivity) {
activity = mainActivity;
}
public void unlink() {
activity = null;
}
}
The best thing you could probably do is put a break point on your task and step through it. It would seem to me (mind you with only just a glance of code to go on) that your ips variable is null by the time your second try block is hit.
If that’s the case, one thing you could try doing is condensing the try blocks into a single one and combine the catch statements (since they all do the same thing anyway) and see if that helps.
Cheers.