错误使用HTTP POST连接 [英] Error with HTTP POST Connection

查看:87
本文介绍了错误使用HTTP POST连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个类来使用HTTP POST和放大器;从通过PHP / JSON数据的基础上获取数据,并将其转换为一个字符串。

问题是,我得到
错误的HTTP连接android.os.NetworkOnMainThreadException在LogCat中。我在模拟器上测试这一点,我使用的是正确的端口地址。我知道这是因为端口工作正常,在应用程序(从本地主机加载HTML)的其他部分。如果我运行在桌面浏览器的PHP文件,它运行良好。我也知道,权限设置是否正确在MySQL 10.0.2.2和放大器;用户在登录的PHP文件中设置。

我的问题是:有没有人翻过了同样的问题,如果是这样,你有没有想出什么问题是或者我应该看什么别的任何想法

日Thnx您的帮助!

 公共无效loadquery子(串P){        串QO = getIntent()getStringExtra(QUERY_ORDER);        ArrayList的<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
        // HTTP POST
        尝试{
            HttpClient的HttpClient的=新DefaultHttpClient();
            // HttpPost httppost =新HttpPost(http://10.0.2.2/Andaero/php/+
            // P + QO +的.php);            //硬codeD php的链接,以确保 - >            HttpPost httppost =新HttpPost(
                    http://10.0.2.2/Andaero/php/regulatory_list_ASC.php);            httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
            HTT presponse响应= httpclient.execute(httppost);
            HttpEntity实体= response.getEntity();
            是= entity.getContent();
        }赶上(例外五){
            Log.e(log_tag,在HTTP连接错误+ e.toString());
        }        //响应转换为字符串
        尝试{
            读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
                    是,ISO-8859-1),8);
            SB =新的StringBuilder();
            sb.append(reader.readLine()+\\ n);            串行=0;
            而((行= reader.readLine())!= NULL){
                sb.append(行+\\ n);
            }
            is.close();
            结果= sb.toString();
        }赶上(例外五){
            Log.e(log_tag,错误转换结果+ e.toString());
        }        setListAdapter(新QueryAdapter(这一点,结果));
    }


解决方案

看来你可能在你的应用程序的主线程执行网络操作(这是对于异常的常见原因被抛出)。

您永远不应该这样做。主线程应仅专用于用户的交互,并且所有的网络交互应卸载到次级线程(例如,使用的AsyncTask)

有关更多信息,很好看的是:
http://developer.android.com/guide/practices/design/responsiveness.html

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

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

这篇关于错误使用HTTP POST连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆