【问题标题】:Android ICS HTTP POSTAndroid ICS HTTP POST
【发布时间】:2012-11-22 22:00:52
【问题描述】:

我知道这个问题已经被问过很多次了。我也搜索了论坛,但无法找到我正在寻找的答案。

该代码适用于许多 android 版本,但不适用于 3.0 或更高版本。字符串 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); 上崩溃应用程序;

你们能建议我,什么或如何解决这个问题?

感谢

【问题讨论】:

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


    【解决方案1】:

    这是因为自从 Honeycomb 以来,您有义务在 UI 线程之外进行网络连接。将您的网络移至 IntentService 或 AsyncTask

    见这篇文章:http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html

    【讨论】:

    • 我可以看一个如何实现这一目标的示例吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    相关资源
    最近更新 更多