0

有人可以帮我优化这个 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)
4

2 回答 2

1

我认为您需要调用executeOnlinetip以在后台线程上启动任务。

在您的SocialTips课程中,您正在调用doInBackground主线程:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1,oltip.doInBackground("social"));

Android 文档,请参阅标题“用法”:

一旦创建,任务就会非常简单地执行:

新的 DownloadFilesTask().execute(url1, url2, url3);

于 2013-05-24T13:48:01.423 回答
0

执行完成后,您将需要使用onPostExecute更新 ListView 。doInBackground为此,您需要使用 Onlinetip类构造函数将ListView当前活动的上下文和当前活动的上下文从以下位置发送到 Onlinetip 类SocialTips

这样做:

首先在类中创建一个参数化的构造函数Onlinetip

ListView listview;
Context context;
public Onlinetip(ListView listview,Context context){
  this.listview=listview;
  this.context=context;
}
...

protected void onPostExecute(ArrayList<String> result) {
         //update ListView Adapter here
       ArrayAdapter<String> adapter =context,android.R.layout.simple_list_item_1); 
       listview.setListAdapter(adapter);
     }

现在将 ListView 和 Context 发送到Onlinetip类:

  public View onCreateView(LayoutInflater inflater, 
                    ViewGroup container, Bundle savedInstanceState){

    ListView listview= this.getListView();
    Onlinetip oltip=new Onlinetip(getActivity(),listview);
    // start AsyncTask here
    oltip.execute("social");

    return super.onCreateView(inflater, 
           container, savedInstanceState); 
  }
于 2013-05-24T14:03:34.310 回答