有人可以帮我优化这个 AsyncTask 代码。我在 android 中进行 JSON 解析,并根据输入“TAG_TYPE”使用不同的数据更新多个列表视图。这是我正在调用的公共类
public class Onlinetip extends AsyncTask<String, Integer, ArrayList<String>> {
ArrayList<NameValuePair> nameValuePairs;
// TAG_TYPE;
HttpPost httppost;
HttpClient httpclient;
HttpResponse response;
HttpEntity entity;
private static final String TAG_CONTENT = "tip_content";
protected ArrayList<String> doInBackground(String... params)
{ ArrayList<NameValuePair> nameValuePairs;
String TAG_TYPE = params[0];
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("API LOCATION");
ArrayList<String> listItems = new ArrayList<String>();
try {
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("tip",TAG_TYPE));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200)
{
entity =response.getEntity();
if(entity !=null)
{
InputStream instream = entity.getContent();
JSONArray jsonResponse = new JSONArray(convertStreamToString(instream));
for(int i = 0; i < jsonResponse.length(); i++)
{
JSONObject c = jsonResponse.getJSONObject(i);
String list = c.getString(TAG_CONTENT);
listItems.add("• "+list);
}
}
}
else{
String error="No Internet connection!";
listItems.add(error);
}
}
catch (JSONException e)
{
String error="No Internet connection!";
listItems.add(error);
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
String error="No Internet connection!";
listItems.add(error);
e.printStackTrace();
} catch (ClientProtocolException e) {
String error="No Internet connection!";
listItems.add(error);
e.printStackTrace();
} catch (IOException e) {
String error="No Internet connection!";
listItems.add(error);
e.printStackTrace();
}
return listItems;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
这是对此类的示例调用。
public class SocialTips extends SherlockListFragment {
Onlinetip oltip=new Onlinetip();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1,oltip.doInBackground("social"));
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
我的 asynctask 是否设置正确?我的 Logcat 中出现跳帧。最终出现 ANR。在 onPostExecution 进行更改后,我收到以下错误
05-25 01:13:37.640: E/AndroidRuntime(698): FATAL EXCEPTION: main
05-25 01:13:37.640: E/AndroidRuntime(698): java.lang.IllegalStateException: Content view not yet created
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
05-25 01:13:37.640: E/AndroidRuntime(698): at com.agrim.lifetips.GamesSports.onCreateView(GamesSports.java:38)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.view.View.measure(View.java:15172)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.view.View.measure(View.java:15172)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-25 01:13:37.640: E/AndroidRuntime(698): at android.view.View.measure(View.java:15172)