简体   繁体   English

HTTP POST连接出错

[英]Error with HTTP POST Connection

I am using a class to use HTTP POST & getting data from a data base via php/json and converting it to a string. 我正在使用一个类来使用HTTP POST并通过php / json从数据库获取数据并将其转换为字符串。

The problem is that I am getting "Error in http connection android.os.NetworkOnMainThreadException" in LogCat. 问题是我在LogCat中遇到"Error in http connection android.os.NetworkOnMainThreadException" 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). 我知道这是因为端口在应用程序的其他部分工作正常(从Localhost加载HTML)。 If I run the php file in a desktop browser, it runs fine. 如果我在桌面浏览器中运行php文件,它运行正常。 I also know that the permissions are set correctly in mySQL for 10.0.2.2 & the user set in the php login file. 我也知道在mySQL for 10.0.2.2中正确设置了权限,并在php登录文件中设置了用户。

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! Thnx对你的帮助!

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() + "\n");

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

        setListAdapter(new QueryAdapter(this, result));
    }

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). 主线程应仅专用于用户交互,并且应将所有网络交互卸载到辅助线程(例如,使用AsyncTask)。

For more info, a good read is: http://developer.android.com/guide/practices/design/responsiveness.html 有关详细信息,请阅读: http//developer.android.com/guide/practices/design/responsiveness.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM