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

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

报告相同问题?

悬赏问题

  • ¥15 vscode问题请教
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM