我尝试将POST数据发送到自己网络上的本地PHP服务器中,并尝试了测试代码。但是由于某种原因该连接被拒绝(即使我的网络还可以),并且我无法将发布数据发送到我的服务器(例如:http://192.168.1.103/bot/post.php) 谁能建议我这是什么导致错误?预先感谢!
public class Main2Activity extends Activity {
private static final String TAG = "Main2Activity.java";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
new PostDataAsyncTask().execute();
}
public class PostDataAsyncTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
try {
postData();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
}
}
// POST the datas
private void postData() {
try {
String postReceiverUrl = "http://192.168.1.103/bot/post.php";
Log.v(TAG, "postURL: " + postReceiverUrl);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(postReceiverUrl);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("abcd", "1234"));
nameValuePairs.add(new BasicNameValuePair("asif", "5678"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
在堆栈跟踪中:
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.103 refused
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:195)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:596)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:517)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:495)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at pericolosopyat.shoppingbot.Main2Activity.postData(Main2Activity.java:73)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at pericolosopyat.shoppingbot.Main2Activity.access$000(Main2Activity.java:23)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at pericolosopyat.shoppingbot.Main2Activity$PostDataAsyncTask.doInBackground(Main2Activity.java:43)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at pericolosopyat.shoppingbot.Main2Activity$PostDataAsyncTask.doInBackground(Main2Activity.java:35)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:316)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at java.lang.Thread.run(Thread.java:776)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: Caused by: java.net.ConnectException: Permission denied
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:192)
2019-06-29 20:16:26.593 29115-29163/pericolosopyat.shoppingbot W/System.err: ... 16 more
2019-06-29 20:16:26.594 29115-29163/pericolosopyat.shoppingbot W/System.err: Caused by: java.net.SocketException: Permission denied
2019-06-29 20:16:26.594 29115-29163/pericolosopyat.shoppingbot W/System.err: at java.net.Socket.createImpl(Socket.java:460)
2019-06-29 20:16:26.594 29115-29163/pericolosopyat.shoppingbot W/System.err: at java.net.Socket.connect(Socket.java:590)
2019-06-29 20:16:26.594 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:124)
2019-06-29 20:16:26.594 29115-29163/pericolosopyat.shoppingbot W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:151)
答案 0 :(得分:0)
它在您的堆栈跟踪Caused by: java.net.SocketException: Permission denied
中说,这意味着您尚未向清单添加适当的权限,只需将<uses-permission android:name="android.permission.INTERNET" />
添加到根AndroidManifest.xml
文件中的application标记之前