dongshi3361 2012-01-06 18:52
浏览 27
已采纳

HTTP POST连接出错

I am using a class to use HTTP POST & getting data from a data base via php/json and converting it to a string.

The problem is that I am getting "Error in http connection android.os.NetworkOnMainThreadException" in LogCat. I'm testing this on the emulator and I am using the correct port address. I know this because the port works fine in other parts of the application(loading HTML from Localhost). If I run the php file in a desktop browser, it runs fine. I also know that the permissions are set correctly in mySQL for 10.0.2.2 & the user set in the php login file.

So my question is: Has anyone come accross the same issue and if so, have you figured out what the problem was or any ideas for what else I should look for?

Thnx for your help!

public void loadQuery(String p) {

        String qO = getIntent().getStringExtra("QUERY_ORDER");

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        // http post
        try {
            HttpClient httpclient = new DefaultHttpClient();
            // HttpPost httppost = new HttpPost("http://10.0.2.2/Andaero/php/" +
            // p + qO + ".php");

            //Hard coded the php link to make sure -->

            HttpPost httppost = new HttpPost(
                    "http://10.0.2.2/Andaero/php/regulatory_list_ASC.php");

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "
");

            String line = "0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "
");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        setListAdapter(new QueryAdapter(this, result));
    }
  • 写回答

1条回答 默认 最新

  • dongyi1111 2012-01-06 18:58
    关注

    It seems you might be doing network operations on your app's main thread (that's a common reason for that exception to be thrown).

    You should never do that. The main thread should be dedicated only to user interaction, and all network interaction should be offloaded to secondary threads (for instance, use AsyncTask).

    For more info, a good read is: http://developer.android.com/guide/practices/design/responsiveness.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度