我正在尝试从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) {
}
}
}
});
答案 0 :(得分:23)
您的清单中是否有INTERNET权限?
检查AndroidManifest.xml
以获取以下行
<uses-permission android:name="android.permission.INTERNET" />
答案 1 :(得分:1)
您是否尝试连接本地计算机?我认为应该是10.0.2.2
见这里:http://developer.android.com/guide/developing/devices/emulator.html#networkaddresses