private class HttpPostTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
HttpURLConnection mHttpCon = null;
String result = null;
try {
mHttpCon = (HttpURLConnection) ((new URL("http://192.168.1.4:8888/about")
.openConnection()));
mHttpCon.setDoOutput(true);
mHttpCon.setRequestProperty("Content-Type", "application/json");
mHttpCon.setRequestMethod("POST");
mHttpCon.connect();
OutputStream mOS = mHttpCon.getOutputStream();
BufferedWriter mWriter = new BufferedWriter(new OutputStreamWriter(mOS, "UTF-8"));
mWriter.write("{'username':'test','fcmid':'fdjslkfjds09f'}");
mWriter.flush();
mWriter.close();
mOS.flush();
mOS.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
我正在使用上面显示的代码,据我所见它根本没有被发送。wireshark甚至不显示我运行应用程序的手机的ip地址发送的任何内容。我可以从iOS应用程序接收请求,但甚至无法从Android发送任何内容。我做错什么了吗?我看过的所有教程都使用了httpclient或androidhttpclient,它们似乎已经不可用了。
最佳答案
@mkocs你要转发buddy,因为自从android m你必须在android中请求关键权限后,permission结构发生了变化。