簡體   English   中英

Android HTTP POST請求錯誤 - 套接字失敗EACCES(權限被拒絕)

[英]Android HTTP POST request error - socket failed EACCES (Permission denied)

我正在嘗試從Eclipse下的Android應用程序向我的localhost發送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) {

                }
        }
    }
});

您的清單中是否有INTERNET權限?

檢查您的AndroidManifest.xml以獲取以下行

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

您是否嘗試連接本地計算機? 我認為而不是127.0.0.1應該是10.0.2.2

請參閱此處: http//developer.android.com/guide/developing/devices/emulator.html#networkaddresses

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM