java – Android HTTP POST请求错误 – 套接字失败EACCES(权限被拒绝)

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

socket failed EACCES (Permission denied).

我是通过apache.commons库做的.我之前试过通过HttpClient连接,但是也有类似的错误:

Connect to myhost refused.

这是代码:

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) {

                }
        }
    }
});

解决方法

您的清单中是否有INTERNET权限?

检查您的AndroidManifest.xml以获取以下行

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

以上是来客网为你收集整理的java – Android HTTP POST请求错误 – 套接字失败EACCES(权限被拒绝)全部内容,希望文章能够帮你解决java – Android HTTP POST请求错误 – 套接字失败EACCES(权限被拒绝)所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。