Android ICS HTTP POST

标签 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); 上使应用程序崩溃

你们能给我一些建议吗?如何解决这个问题?

谢谢

最佳答案

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

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

关于Android ICS HTTP POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13520549/

相关文章:

android - 每天设置五个闹钟并在单击按钮时更新它们

java - 我遇到的最奇怪的 Java 问题,NoClassDefFoundError 由于注释?

c# - 使用带有 HTTPS 证书的 System.Net.WebClient

Android 3.0 Honeycomb 在解析 RSS feed 时的问题

Android addView 将 TextView textColor 设置为白色?

Android 人脸检测 - Vision API 或 Camera API

javascript - 在 azure 函数 (js) 中,异步/等待调用后的 POST 请求不起作用

go - 如何设置和解析正文请求中的时间?

android - hive 应用,LocalBroadcastManager

android - 将硬件加速标志与 Canvas.clipPath 一起使用