Force close when trying to parse JSON with AsyncTask in the background
i'm new to android development and i'm playing around with json data. I managed to get the parsing to work. I want to show a ProgressDialog and i read that i need to use AsyncTask that. But for some reason i get a force close as soon as i put the same working code inside doInBackground() eventhough eclipse says everything is fine.
Here is the source code:
public class HomeActivity extends Activity {
public class BackgroundAsyncTask extends AsyncTask<Void, Integer, Void> {
ProgressDialog dialog = new ProgressDialog (HomeActivity.this);
@Override
protected void onPreExecute() {
dialog.setMessage("Loading...please wait");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected void onPostExecute() {
dialog.dismiss();
}
@Override
protected Void doInBackground(Void... params) {
try {
URL json = new URL("http://www.corps-marchia.de/jsontest.php");
URLConnection tc = json.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
JSONObject jo = (JSONObject) ja.get(0);
TextView txtView = (TextView)findViewById(R.id.TextView01);
txtView.setText(jo.getString("text"));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new BackgroundAsyncTask().execute();
}
}
Here is the error log:
01-08 12:33:48.225: ERROR/AndroidRuntime(815): FATAL EXCEPTION: AsyncTask #1
01-08 12:33:48.225: ERROR/AndroidRuntime(815): java.lang.RuntimeException: An error occured while executing doInBackground()
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at java.lang.Thread.run(Thread.java:1019)
01-08 12:33:48.225: ERROR/AndroidRu开发者_如何学JAVAntime(815): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.view.View.requestLayout(View.java:8267)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.view.View.requestLayout(View.java:8267)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.view.View.requestLayout(View.java:8267)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.view.View.requestLayout(View.java:8267)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.view.View.requestLayout(View.java:8267)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.widget.TextView.checkForRelayout(TextView.java:5521)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.widget.TextView.setText(TextView.java:2724)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.widget.TextView.setText(TextView.java:2592)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.widget.TextView.setText(TextView.java:2567)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at net.ajzele.demo.andy1.HomeActivity$BackgroundAsyncTask.doInBackground(HomeActivity.java:52)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at net.ajzele.demo.andy1.HomeActivity$BackgroundAsyncTask.doInBackground(HomeActivity.java:1)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-08 12:33:48.225: ERROR/AndroidRuntime(815): ... 4 more
01-08 12:33:51.605: ERROR/WindowManager(815): Activity net.ajzele.demo.andy1.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4051d0c0 that was originally added here
01-08 12:33:51.605: ERROR/WindowManager(815): android.view.WindowLeaked: Activity net.ajzele.demo.andy1.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4051d0c0 that was originally added here
01-08 12:33:51.605: ERROR/WindowManager(815): at android.view.ViewRoot.<init>(ViewRoot.java:258)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.app.Dialog.show(Dialog.java:241)
01-08 12:33:51.605: ERROR/WindowManager(815): at net.ajzele.demo.andy1.HomeActivity$BackgroundAsyncTask.onPreExecute(HomeActivity.java:33)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.os.AsyncTask.execute(AsyncTask.java:391)
01-08 12:33:51.605: ERROR/WindowManager(815): at net.ajzele.demo.andy1.HomeActivity.onCreate(HomeActivity.java:72)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.os.Looper.loop(Looper.java:123)
01-08 12:33:51.605: ERROR/WindowManager(815): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-08 12:33:51.605: ERROR/WindowManager(815): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 12:33:51.605: ERROR/WindowManager(815): at java.lang.reflect.Method.invoke(Method.java:507)
01-08 12:33:51.605: ERROR/WindowManager(815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-08 12:33:51.605: ERROR/WindowManager(815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-08 12:33:51.605: ERROR/WindowManager(815): at dalvik.system.NativeStart.main(Native Method)
Any hints? I hope you can help me out ive searched the net and didnt find any working solution...Thanks in advance
Check the docs for AsyncTask. You can't directly manipulate the UI from within doInBackground
. In order to make UI updates, you need to call publishProgress
. You will also need to change the signature of your AsyncTask
to pass a String
. So something like this:
public class BackgroundAsyncTask extends AsyncTask<Void, String, Void> {
TextView txtView = (TextView)findViewById(R.id.TextView01);
...
protected void onProgressUpdate(String... message) {
txtView.setText(message[0]);
}
...
@Override
protected Void doInBackground(Void... params) {
try {
URL json = new URL("http://www.corps-marchia.de/jsontest.php");
URLConnection tc = json.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
JSONObject jo = (JSONObject) ja.get(0);
publishProgress(jo.getString("text"));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
You are not allowed to update GUI elements from a non UI/Main Thread
txtView.setText(jo.getString("text"));
Handling Expensive Operations in the UI Thread
Or use
onPostExecute
精彩评论