【问题标题】:Android HTTP POST request error - socket failed EACCES (Permission denied)Android HTTP POST 请求错误 - 套接字 EACCES 失败(权限被拒绝)
【发布时间】:2012-03-16 17:52:48
【问题描述】:

我正在尝试从Eclipse 下的 Android 应用程序向我的本地主机发送 POST 请求,但我收到此错误:

套接字 EACCES 失败(权限被拒绝)。

我正在通过 apache.commons 库执行此操作。我之前尝试过通过HttpClient连接,但是出现了类似的错误:

连接到 myhost 被拒绝。

代码如下:

    public void onClick(View v) {
        login = (EditText) findViewById(R.id.entry_login);
        userLogin = login.getText().toString();

        pwd = (EditText) findViewById(R.id.entry_password);
        userPwd = pwd.getText().toString();

        BufferedReader br = null;

        HttpClient httpclient = new HttpClient();

        PostMethod method = new PostMethod("http://127.0.0.1/testPost.php");
        method.addParameter("name", "Arthur");

        System.out.println("Login: " + userLogin);

        try {
            httpclient.executeMethod(method);

            int returnCode = httpclient.executeMethod(method);

            if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                System.err.println("The Post method is not implemented by this URI");

                // Still consume the response body
                method.getResponseBodyAsString();
            }
            else {
                br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
                String readLine;
                while (((readLine = br.readLine()) != null)) {
                    System.err.println(readLine);
                }
            }

            /* List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("name", "Arthur"));
                nameValuePairs.add(new BasicNameValuePair("OP_ID", "10001"));
                nameValuePairs.add(new BasicNameValuePair("IP_ADDRESS", "127.0.0.1"));
                nameValuePairs.add(new BasicNameValuePair("FIELDS=field100", userLogin + "&field101=" + userPwd));
                nameValuePairs.add(new BasicNameValuePair("REQ_TYPE=", "26"));
            */

            System.out.println("http connection done well!");
            // response.getStatusLine();

        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            method.releaseConnection();
            if (br != null)
                try {
                    br.close();
                }
                catch (Exception fe) {

                }
        }
    }
});

【问题讨论】:

    标签: java android


    【解决方案1】:

    您的清单中有 INTERNET 权限吗?

    检查您的AndroidManifest.xml 是否有以下行

    <uses-permission android:name="android.permission.INTERNET" />
    

    【讨论】:

    • 好建议老兄之前的错误信息消失了,但 NullPointerException 现在实际 =),1 指向 U。
    • 出现错误 FATAL EXCEPTION: main; android.os.NetworkOnMainThreadException; android.BlockGuardPolicy.onNetwork 等我认为模拟器仍然通过套接字阻塞请求......
    • 在 NetworkOnMainThreadException 上查看我的答案 - stackoverflow.com/questions/9380166/fatal-exception-in-android/…
    【解决方案2】:

    您是否正在尝试连接到本地计算机?我认为应该是 10.0.2.2

    而不是 127.0.0.1

    请看这里:http://developer.android.com/guide/developing/devices/emulator.html#networkaddresses

    【讨论】:

    • 感谢您的建议,但它对我不起作用 - 出现了相同的消息。另外我想知道如何将 POST 请求发送到其他“真实”主机(不仅是本地)?
    猜你喜欢
    • 2023-03-09
    • 2019-04-03
    • 2012-07-01
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    相关资源
    最近更新 更多