本文介绍了Android的HTTP POST请求错误 - 套接字失败EACCES(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个POST请求从一个Android应用程序在 Eclipse的发到我的本地主机,但我收到此错误:

我这样做是通过apache.commons库。我试图通过HttpClient的前连接,但有一个类似的错误:

下面是code:

 公共无效的onClick(视图v){
        登录=(EditText上)findViewById(R.id.entry_login);
        。用户登陆= login.getText()的toString();

        PWD =(EditText上)findViewById(R.id.entry_password);
        。userPwd = pwd.getText()的toString();

        的BufferedReader BR = NULL;

        HttpClient的HttpClient的=新的HttpClient();

        的PostMethod方法=新的PostMethod(http://127.0.0.1/testPost.php);
        method.addParameter(姓名,亚瑟);

        的System.out.println(登录:+用户登陆);

        尝试 {
            httpclient.executeMethod(法);

            int返回code = httpclient.executeMethod(法);

            如果(返程code == HttpStatus.SC_NOT_IMPLEMENTED){
                通信System.err.println(POST方法不受此URI执行);

                //仍然消耗响应体
                method.getResponseBodyAsString();
            }
            其他 {
                BR =新的BufferedReader(新的InputStreamReader(method.getResponseBodyAsStream()));
                字符串的readLine;
                而(((的readLine = br.readLine())!= NULL)){
                    通信System.err.println(的readLine);
                }
            }

            / *列表与LT;的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(2);
                nameValuePairs.add(新BasicNameValuePair(姓名,亚瑟));
                nameValuePairs.add(新BasicNameValuePair(OP_ID,10001));
                nameValuePairs.add(新BasicNameValuePair(IP_ADDRESS,127.0.0.1));
                nameValuePairs.add(新BasicNameValuePair(栏= field100,用户登陆+&放大器; field101 =+ userPwd));
                nameValuePairs.add(新BasicNameValuePair(REQ_TYPE =,26));
            * /

            的System.out.println(HTTP连接做得很好!);
            // response.getStatusLine();

        }
        赶上(例外五){
            的System.out.println(e.getMessage());
        }
        最后 {
            method.releaseConnection();
            如果(BR!= NULL)
                尝试 {
                    br.close();
                }
                赶上(例外FE){

                }
        }
    }
});
 

解决方案

你有INTERNET权限您的清单?

检查的Andr​​oidManifest.xml 的followinf行

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
 

I'm trying to send a POST request to my localhost from an Android app under Eclipse, but I am receiving this error:

I'm doing this through the apache.commons library. I've tried to connect via HttpClient before, but there was a similar error:

Here is the code:

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

                }
        }
    }
});
解决方案

Do you have INTERNET permissions in your manifest?

Check your AndroidManifest.xml for the followinf lines

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

这篇关于Android的HTTP POST请求错误 - 套接字失败EACCES(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:22