Android ICS HTTP POST

时间:2012-11-22 22:00:53

标签: android http-post android-3.0-honeycomb ice

我知道这个问题已被多次询问过。我也在论坛上搜索过,但无法找到我要找的答案。

代码适用于许多Android版本,但不适用于3.0或更高版本。 String response_from_server = null;

    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(file_n_folder.URL_POST);

    //Log.d("URL",file_n_folder.URL_POST);

    try {

        JSONArray postjson = new JSONArray();
        postjson.put(obj);

        // Post the data:
        httppost.setHeader("json",obj.toString());
        httppost.getParams().setParameter("jsonpost",postjson);

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        // for JSON:
        if(response != null){

            InputStream is = response.getEntity().getContent();

            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();
                }
            }
            response_from_server = sb.toString();

            //Log.d("RESPONSE FROM SERVER",response_from_server);

            return response_from_server;
        }

    }catch (ClientProtocolException e){
        Log.d("EXCEPTION line 94",e.getMessage());
    }catch (IOException e){
        Log.d("EXCEPTION line 96",e.getMessage());
    }
    return response_from_server;

它总是在httpclient.execute(httppost);

上崩溃应用程序

你能告诉我,这是什么或如何解决这个问题?

感谢名单

1 个答案:

答案 0 :(得分:1)

这是因为Honeycomb你有义务在UI线程之外进行网络连接。将您的网络移动到IntentService或AsyncTask

请参阅此文章:http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html