我是android初学者,并尝试使用json获取pnr状态这里是我的代码,这是无效的请帮助我。检查控制台时有一些空的异常它说doinbackground方法有一些问题不明白它说的是什么...
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)
答案 0 :(得分:4)
您在.setText()
中的某些TextView上调用PNR()
,然后您从doInBackground(). setText()
调用的是UI操作,并且doInBackground()
不允许触摸UI线程。
如果你想在AsyncTask中进行一些UI操作,你应该在onPreExecute()
或onPostExecute()
中进行操作。