Reputation: 37
I am android beginner and trying to fetch pnr status using json here is my code which is not working please help me . There is some null exception when checking the console it said there is some problem in doinbackground method do not understand what it said...
public class Riki extends Activity {
TextView httpStuff;
HttpClient client;
JSONObject json;
final static String URL = "http://www.railpnrapi.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httpexample);
httpStuff = (TextView) findViewById(R.id.tvHttp);
client = new DefaultHttpClient();
new Read().execute("tnum");
}
public JSONObject PNR(String username)
throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(username);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
httpStuff.setText(url);
httpStuff.setText(status);
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray timeline = new JSONArray(data);
JSONObject last = timeline.getJSONObject(0);
return last;
} else {
Toast.makeText(Riki.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
json = PNR("8506503026");
return json.getString(params[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
httpStuff.setText(result);
}
}
}
12-06 16:28:48.180: E/AndroidRuntime(2871): FATAL EXCEPTION: AsyncTask #2
12-06 16:28:48.180: E/AndroidRuntime(2871): java.lang.RuntimeException: An error occured while executing doInBackground()
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-06 16:28:48.180: E/AndroidRuntime(2871): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-06 16:28:48.180: E/AndroidRuntime(2871): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-06 16:28:48.180: E/AndroidRuntime(2871): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-06 16:28:48.180: E/AndroidRuntime(2871): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-06 16:28:48.180: E/AndroidRuntime(2871): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-06 16:28:48.180: E/AndroidRuntime(2871): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-06 16:28:48.180: E/AndroidRuntime(2871): at java.lang.Thread.run(Thread.java:1019)
12-06 16:28:48.180: E/AndroidRuntime(2871): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.view.ViewRoot.checkThread(ViewRoot.java:3011)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.view.ViewRoot.requestLayout(ViewRoot.java:630)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.view.View.requestLayout(View.java:8268)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.view.View.requestLayout(View.java:8268)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.view.View.requestLayout(View.java:8268)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.view.View.requestLayout(View.java:8268)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.view.View.requestLayout(View.java:8268)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.widget.ScrollView.requestLayout(ScrollView.java:1289)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.view.View.requestLayout(View.java:8268)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.widget.TextView.checkForRelayout(TextView.java:5547)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.widget.TextView.setText(TextView.java:2730)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.widget.TextView.setText(TextView.java:2598)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.widget.TextView.setText(TextView.java:2573)
12-06 16:28:48.180: E/AndroidRuntime(2871): at com.example.coder.Riki.PNR(Riki.java:49)
12-06 16:28:48.180: E/AndroidRuntime(2871): at com.example.coder.Riki$Read.doInBackground(Riki.java:69)
12-06 16:28:48.180: E/AndroidRuntime(2871): at com.example.coder.Riki$Read.doInBackground(Riki.java:1)
12-06 16:28:48.180: E/AndroidRuntime(2871): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-06 16:28:48.180: E/AndroidRuntime(2871): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
Upvotes: 0
Views: 174
Reputation: 1072
You're calling .setText()
on some TextViews in PNR()
, which you're then calling from doInBackground(). setText()
is a UI operation and doInBackground()
is not allowed to touch the UI thread.
If you want to do some UI manipulation from within an AsyncTask, you should be doing it in onPreExecute()
or onPostExecute()
.
Upvotes: 4