安卓HTTP POST请求出错 - 套接字失败EACCES(权限被拒绝)

6
我正在尝试从Eclipse下的Android应用向本地主机发送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) {

                }
        }
    }
});
2个回答

23

您在清单文件中是否拥有INTERNET权限?

请检查您的AndroidManifest.xml文件是否包含以下行

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

好的建议伙计,之前的错误信息消失了,但现在出现了NullPointerException =),给你一分。 - Arthur Kushman
存在错误 FATAL EXCEPTION: main; android.os.NetworkOnMainThreadException; android.BlockGuardPolicy.onNetwork 等,我认为模拟器仍在阻止通过 sockets 请求。 - Arthur Kushman
关于NetworkOnMainThreadException的问题,请参考我的回答 - http://stackoverflow.com/questions/9380166/fatal-exception-in-android/9380512#9380512 - Olegas

1

谢谢您的建议,但对我没有用 - 同样的消息出现了。此外,我想知道如何向其他“真实”主机发送POST请求(不仅仅是本地)? - Arthur Kushman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接