先上代码,我估计是最简单的一个发起通信的请求了吧(然而萌新真的很懵)。
public void sendRequestWithHttp(){
new Thread(new Runnable() {
HttpURLConnection httpURLConnection;
JSONObject jsonObject=new JSONObject();
@Override
public void run() {
try{
jsonObject.put("CarId",1);
URL url=new URL("http://192.168.1.108:8080/transportservice/type/jason/action/GetCarSpeed");
httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setReadTimeout(8000);
httpURLConnection.setUseCaches(false);
DataOutputStream dataOutputStream=new DataOutputStream(httpURLConnection.getOutputStream());
String message=String.valueOf(jsonObject);
dataOutputStream.writeBytes(message);
dataOutputStream.flush();
dataOutputStream.close();
httpURLConnection.disconnect();
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
}
emmmm大佬们,我现在只想向服务器发送数据,只要发过去就好QwQ
然后无论我怎么点击发送按钮(上方代码放在按钮事件中),服务器都没有一丝丝的反应(url地址是没问题的,这个可以放心)。
Setting device_provisioned has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
jank_removeInvalidNode all the node in jank list is out of time
(尤其是第二个,报的最多,我百度了好久也没明白什么意思)
(顺便问一下大佬,这个HttpURLConnection是从哪一步开始向服务器发起连接请求及数据的---比如OkHttp里面有一句
Request request=new Request.Builder().url("http://192.168.1.108:8080/transportservice/type/jason/action/GetCarSpeed").post(requestBody).build();
起这个作用的。
)
下面是修改后的代码,至于原因—大概就是不同的那一行出的问题吧
JSONObject jsonObject=new JSONObject();
jsonObject.put("CarId",1);
URL url=new URL("http://192.168.1.108:8080/transportservice/type/jason/action/GetCarSpeed");
httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Content-Type","application/json");
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setReadTimeout(500000);
httpURLConnection.setConnectTimeout(8000);
httpURLConnection.setUseCaches(false);
httpURLConnection.getOutputStream().write(String.valueOf(jsonObject).getBytes());
int code=httpURLConnection.getResponseCode();
httpURLConnection.disconnect();
总而言之,只修改了两个地方: 一个是读数据的时间,一个是发送数据的方式。