私は春のブートで私の残りのAPIにjsonを送る必要があります。 それは次のようになります。アンドロイドからpost APIへの投稿リクエストを送信するには?
{
"deviceid":543 ,
"lat": 56.78,
"lon": 67.45,
"date": 1501624800000,
"time": 18000000
}
私はGoogleのボレーを使用しています。
[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形式に変換する方法です。
ありがとうございます。
Android用REST APIを使用するためのスプリングガイドをご覧ください。 https://spring.io/guides/gs/consuming-rest-android/ – theCakeCoder
JSONとの間でデータを変換するには、変数を使用せずにシリアル化可能なデータクラスを作成します。 https://stackoverflow.com/questions/7539954/java-json-serialization-best-practice – theCakeCoder