我在android中应用带有json解析的asynctask,但它可以显示错误。我的代码是
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
private static final String url = "http://203.110.93.84/trainee/yoga_api.php?category";
// JSON Node names
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
private static final String TAG_ID = "id";
private static final String TAG_NAME = "yoga_name";
private static final String TAG_CATEGORY = "category";
private static final String TAG_DESC = "yoga_desc";
private static final String TAG_URL = "yoga_url";
String gender;
// contacts JSONArray
JSONArray contacts = null;
Utility utility=new Utility();
String json;
private static String newurl="";
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
cd = new ConnectionDetector(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
//showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection","You have internet connection", true);
Intent myIntent = getIntent();
newurl=url+"="+myIntent.getSerializableExtra("catname").toString();
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(AndroidJSONParsingActivity.this, "No Internet Connection",
"You don't have internet connection.", false);
}
new Async().execute();
/*BackgroundAsyncTask task= new BackgroundAsyncTask();
task.execute();*/
// Creating JSON Parser instance
// JSONParser jParser = new JSONParser();
/**
* Updating parsed JSON data into ListView
* */
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_DESC, cost);
in.putExtra(TAG_URL, description);
startActivity(in);
}
});
}
class Async extends AsyncTask<String, Integer, String> {
ProgressDialog dialog;
protected void onPreExecute()
{
dialog=new ProgressDialog(AndroidJSONParsingActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Loading");
dialog.setMessage("Please Wait....");
dialog.show();
}
@Override
protected String doInBackground(String... params) {
// perform desired task in this doInBackground Block
jsonparser();
return "";
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
// dialog.incrementProgressBy(5);
}
@Override
protected void onPostExecute(String result)
{
dialog.dismiss();
}
}
public void jsonparser()
{
// getting JSON string from URL
JSONObject c=null;
try {
json = utility.getResponse(newurl);
// Toast.makeText(getApplicationContext(),json, Toast.LENGTH_LONG).show();
contacts = new JSONArray(json);
for(int i = 0; i < contacts.length(); i++){
c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_DESC);
gender = c.getString(TAG_URL);
//Toast.makeText(getApplicationContext(),id, Toast.LENGTH_LONG).show();
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_DESC, address);
map.put(TAG_URL, gender);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_DESC, TAG_URL }, new int[] {
R.id.name, R.id.email, R.id.mobile });
setListAdapter(adapter);
}
}
我的logcat是
02-23 16:16:16.616: D/dalvikvm(946): GC_FOR_MALLOC freed 2209 objects / 129344 bytes in 77ms
02-23 16:16:16.736: W/dalvikvm(946): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
02-23 16:16:16.756: E/AndroidRuntime(946): FATAL EXCEPTION: AsyncTask #1
02-23 16:16:16.756: E/AndroidRuntime(946): java.lang.RuntimeException: An error occured while executing doInBackground()
02-23 16:16:16.756: E/AndroidRuntime(946): at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-23 16:16:16.756: E/AndroidRuntime(946): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-23 16:16:16.756: E/AndroidRuntime(946): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-23 16:16:16.756: E/AndroidRuntime(946): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-23 16:16:16.756: E/AndroidRuntime(946): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-23 16:16:16.756: E/AndroidRuntime(946): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
02-23 16:16:16.756: E/AndroidRuntime(946): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
02-23 16:16:16.756: E/AndroidRuntime(946): at java.lang.Thread.run(Thread.java:1096)
02-23 16:16:16.756: E/AndroidRuntime(946): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
02-23 16:16:16.756: E/AndroidRuntime(946): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
02-23 16:16:16.756: E/AndroidRuntime(946): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
02-23 16:16:16.756: E/AndroidRuntime(946): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
02-23 16:16:16.756: E/AndroidRuntime(946): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
02-23 16:16:16.756: E/AndroidRuntime(946): at android.view.View.invalidate(View.java:5139)
02-23 16:16:16.756: E/AndroidRuntime(946): at android.widget.AbsListView.resetList(AbsListView.java:1011)
02-23 16:16:16.756: E/AndroidRuntime(946): at android.widget.ListView.resetList(ListView.java:493)
02-23 16:16:16.756: E/AndroidRuntime(946): at android.widget.ListView.setAdapter(ListView.java:422)
02-23 16:16:16.756: E/AndroidRuntime(946): at android.app.ListActivity.setListAdapter(ListActivity.java:267)
02-23 16:16:16.756: E/AndroidRuntime(946): at com.callplanets.app.AndroidJSONParsingActivity.jsonparser(AndroidJSONParsingActivity.java:204)
02-23 16:16:16.756: E/AndroidRuntime(946): at com.callplanets.app.AndroidJSONParsingActivity$Async.doInBackground(AndroidJSONParsingActivity.java:138)
02-23 16:16:16.756: E/AndroidRuntime(946): at com.callplanets.app.AndroidJSONParsingActivity$Async.doInBackground(AndroidJSONParsingActivity.java:1)
02-23 16:16:16.756: E/AndroidRuntime(946): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-23 16:16:16.756: E/AndroidRuntime(946): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-23 16:16:16.756: E/AndroidRuntime(946): ... 4 more
02-23 16:16:17.376: E/WindowManager(946): Activity com.callplanets.app.AndroidJSONParsingActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44ea7f00 that was originally added here
02-23 16:16:17.376: E/WindowManager(946): android.view.WindowLeaked: Activity com.callplanets.app.AndroidJSONParsingActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44ea7f00 that was originally added here
02-23 16:16:17.376: E/WindowManager(946): at android.view.ViewRoot.<init>(ViewRoot.java:247)
02-23 16:16:17.376: E/WindowManager(946): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-23 16:16:17.376: E/WindowManager(946): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-23 16:16:17.376: E/WindowManager(946): at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-23 16:16:17.376: E/WindowManager(946): at android.app.Dialog.show(Dialog.java:241)
02-23 16:16:17.376: E/WindowManager(946): at com.callplanets.app.AndroidJSONParsingActivity$Async.onPreExecute(AndroidJSONParsingActivity.java:132)
02-23 16:16:17.376: E/WindowManager(946): at android.os.AsyncTask.execute(AsyncTask.java:391)
02-23 16:16:17.376: E/WindowManager(946): at com.callplanets.app.AndroidJSONParsingActivity.onCreate(AndroidJSONParsingActivity.java:82)
02-23 16:16:17.376: E/WindowManager(946): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-23 16:16:17.376: E/WindowManager(946): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-23 16:16:17.376: E/WindowManager(946): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-23 16:16:17.376: E/WindowManager(946): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-23 16:16:17.376: E/WindowManager(946): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-23 16:16:17.376: E/WindowManager(946): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 16:16:17.376: E/WindowManager(946): at android.os.Looper.loop(Looper.java:123)
02-23 16:16:17.376: E/WindowManager(946): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-23 16:16:17.376: E/WindowManager(946): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 16:16:17.376: E/WindowManager(946): at java.lang.reflect.Method.invoke(Method.java:521)
02-23 16:16:17.376: E/WindowManager(946): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-23 16:16:17.376: E/WindowManager(946): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-23 16:16:17.376: E/WindowManager(946): at dalvik.system.NativeStart.main(Native Method)
02-23 16:16:20.286: D/dalvikvm(956): GC_EXTERNAL_ALLOC freed 1053 objects / 70784 bytes in 83ms
请帮我看看示例代码。
我是android的初学者..
提前致谢