2017-08-03 124 views
-1

我需要在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格式?

謝謝你的時間。

+0

查看Spring使用指南,瞭解Android的REST API。 https://spring.io/guides/gs/consuming-rest-android/ – theCakeCoder

+0

爲了將數據轉換爲JSON或從JSON轉換數據,請使用可序列化的數據類,而不是使用變量。 https://stackoverflow.com/questions/7539954/java-json-serialization-best-practice – theCakeCoder

回答

0

我解決了我的問題的一部分,但還有一個。 我發送數據到數據庫。 當位置發生變化時,而不是放置新的位置,舊位置將被覆蓋。 數據庫中的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 
} 
相關問題