如何发送来自android的发布请求来休息api?
问题描述:
我需要在spring启动时将json发送给我的其余api。 它应该是这样的:如何发送来自android的发布请求来休息api?
{
"deviceid":543 ,
"lat": 56.78,
"lon": 67.45,
"date": 1501624800000,
"time": 18000000
}
我使用谷歌抽射。 我得到一个错误,在android系统监测:
[2794] BasicNetwork.performRequest: Unexpected response code 400 for
http://192.168.0.137:8080/coordinates
这是错误的形式STS-春天控制台:
2017-08-03 11:01:21.537 WARN 5056 --- [nio-8080-exec-
6].w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException:
Could not read document: Unrecognized token 'date': was expecting
('true','false' or 'null')at [Source:[email protected];
line: 1, column: 6]; nested exception is
com.fasterxml.jackson.core.JsonParseException: Unrecognized token
'date': was expecting ('true', 'false' or 'null')at [Source:
[email protected]; line: 1, column: 6]
这是我的GPS服务代码。
public class GPS_Service extends Service {
private LocationListener locationListener;
private LocationManager locationManager;
String insertUrl = "http://192.168.0.137:8080/coordinates";
RequestQueue requestQueue;
String la_string,lo_string;
String deviceid = "987";
String date="1501624800000";
String time ="61200000";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
locationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
Intent i = new Intent("location_update");
//i.putExtra("coordinates",location.getLongitude()+" "+location.getLatitude());
//Intent i1 = new Intent("latitude_update");
//Intent i2 = new Intent("longitude_update");
i.putExtra("latitude", location.getLatitude());
i.putExtra("longitude", location.getLongitude());
sendBroadcast(i);
la_string=Double.toString(location.getLatitude());
lo_string=Double.toString(location.getLongitude());
requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> parameters = new HashMap<String, String>();
parameters.put("deviceid",deviceid);
parameters.put("lat",la_string);
parameters.put("lon",lo_string);
parameters.put("date","1501624800000");
parameters.put("time","18000000");
return parameters;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("Content-Type", "application/json; charset=utf-8");
return parameters;
}
};
requestQueue.add(request);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,locationListener);
}
@Override
public void onDestroy() {
super.onDestroy();
if(locationManager != null){
//noinspection MissingPermission
locationManager.removeUpdates(locationListener);
}
}}
正如你看到的日期和时间是硬编码的,我的下一个问题是如何将日期和时间转换成JSON格式?
谢谢你的时间。
答
我解决了我的问题的一部分,但还有一个。 我发送数据到数据库。 当位置发生变化时,而不是放置新的位置,旧位置将被覆盖。 数据库中的id字段是自动增量的。 我的帖子请求看起来像。
requestQueue = Volley.newRequestQueue(getApplicationContext());
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("deviceid", "1919");
jsonParams.put("lat", la_string);
jsonParams.put("lon", lo_string);
jsonParams.put("date","1501624800000");
jsonParams.put("time","18000000");
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, insertUrl,
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle Error
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", System.getProperty("http.agent"));
return headers;
}
};
requestQueue.add(postRequest);
Android发送0作为id的vlue,并覆盖该行。 当我使用POSTMAN生成发布请求并且不在请求数据库中放置id字段时,会自动递增id字段,并放入下一行。 POSTMAN的发布请求如下所示:
{
"deviceid":455466 ,
"lat": 56.78,
"lon": 67.45,
"date": 1501624800000,
"time": 18000000
}
查看Spring使用指南,了解Android的REST API。 https://spring.io/guides/gs/consuming-rest-android/ – theCakeCoder
为了将数据转换为JSON或从JSON转换数据,请使用可序列化的数据类,而不是使用变量。 https://*.com/questions/7539954/java-json-serialization-best-practice – theCakeCoder