2012-02-27 14 views
6

मैं Eclipse के तहत एक Android ऐप्लिकेशन से मेरी स्थानीय होस्ट करने के लिए एक पोस्ट अनुरोध भेजने के लिए कोशिश कर रहा हूँ, लेकिन मैं इस त्रुटि मिल रही है:एंड्रॉयड HTTP POST अनुरोध त्रुटि - सॉकेट में विफल रहा है EACCES (अनुमति अस्वीकृत)

socket failed EACCES (Permission denied).

मैं मैं apache.commons पुस्तकालय के माध्यम से कर रहा हूँ। मैंने पहले HttpClient के माध्यम से कनेक्ट करने की कोशिश की है, लेकिन इसी तरह की एक त्रुटि थी:

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

       } 
     } 
    } 
}); 

उत्तर

23

आप अपने मेनिफ़ेस्ट में इंटरनेट अनुमतियों है? एक ही संदेश दिखाई -

अपने followinf लाइनों

सलाह के लिए
<uses-permission android:name="android.permission.INTERNET" /> 
+0

अच्छा सलाह संदेश पिछले त्रुटि संदेश चला गया है, लेकिन NullPointerException वास्तविक अब =), 1 बिंदु –

+0

में एक त्रुटि है FATAL EXCEPTION: मुख्य; android.os.NetworkOnMainThreadException; android.BlockGuardPolicy.onNetwork आदि मुझे लगता है कि एमुलेटर अभी भी सॉकेट के माध्यम से अनुरोध अवरुद्ध कर रहा है ... –

+0

NetworkOnMainThreadException पर मेरा जवाब यहां देखें - http://stackoverflow.com/questions/9380166/fatal-exception-in-android/9380512#9380512 – Olegas

1

आप अपने स्थानीय मशीन से कनेक्ट करने के लिए कोशिश कर रहे हैं:

Connect to myhost refused.

यहाँ कोड है? मैं 127.0.0.1 के बजाय लगता है कि यह होना चाहिए 10.0.2.2

यहाँ देखें: http://developer.android.com/guide/developing/devices/emulator.html#networkaddresses

+0

धन्यवाद के लिए AndroidManifest.xml है, लेकिन यह मेरे लिए काम didn 't की जाँच करें। मुझे आश्चर्य है कि अन्य "असली" मेजबानों के लिए POST अनुरोध कैसे भेजें (न केवल स्थानीय)? –

संबंधित मुद्दे