使用HttpClient的Android网络需要花费很多时间

时间:2014-03-03 09:37:58

标签: android http android-networking

当我尝试使用HttpCleitn从我的服务器获取数据时,我遇到了问题需要花费很多时间(大约30秒,响应只有几KB)并且它不断记录消息:“Skippe xxx frames.App可能在其主线上做了太多工作“。只有很多这些消息(如1000或2000)。 我是Android开发人员的新手,不知道我做错了什么。虽然从服务器获取数据的所有过程都保存在doInBackground方法中。

这是我的所有代码

public class MainActivity extends Activity {

ListView videoList;
ArrayList<String> videoArrayList;
ArrayAdapter<String> videoAdapter;
Context context;
String feedUrl = "http://www.rateksib.ru/ios/track_json.php?order=НскС101712124";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = this;

    videoArrayList = new ArrayList<String>();
    videoList = (ListView) findViewById(R.id.videoList);
    videoAdapter = new ArrayAdapter<String>(this, R.layout.video_list_item, videoArrayList);
    videoList.setAdapter(videoAdapter);

    VideoListTask loaderTask = new VideoListTask();
    loaderTask.execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class VideoListTask extends AsyncTask<Void, Void, Void> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(context);
        dialog.setTitle("Выполняется запрос");
        dialog.show();
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
        videoAdapter.notifyDataSetChanged();
        super.onPostExecute(result);

    }

    @Override
    protected Void doInBackground(Void... params) {
        HttpClient client = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(feedUrl);

        try 
        {
            HttpResponse response = client.execute(getRequest);
            StatusLine statusLine = response.getStatusLine();
            if( statusLine.getStatusCode() != 200 ) {
                return null;
            }

            InputStream jsonStream = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(jsonStream));
            StringBuilder stringBuilder = new StringBuilder();

            String line;
            while( (line = reader.readLine()) != null ) {
                stringBuilder.append(line);
            }

            String jsonData = stringBuilder.toString();

            JSONObject json = new JSONObject(jsonData);
            JSONArray results = json.getJSONArray("results");

            for( int i = 0; i < results.length(); i++ ) {
                JSONObject item = results.getJSONObject(i);

                String status = item.getString("status");
                String date = item.getString("date");

                Log.i("OK:", "Detsya");

                videoArrayList.add(date + ": " + status);
            }



        } 
        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;
    }


}

}

0 个答案:

没有答案