如何在Android和JSON / POST中使用SpringFramework?

时间:2012-11-28 20:09:40

标签: java android json

我已经阅读并尝试了互联网上的所有解决方案,并且他们似乎都为我做了一个例外。我成功地做了一个GET,但似乎无法让POST工作。

这是我目前的代码:

    String url = new String("http://www.myurl.com/scripts/json/v1/slipmanager.php");

    MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
    formData.add("username", userName);
    formData.add("password", userPassword);
    formData.add("method", "getslips");

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
    RestTemplate restTemplate = new RestTemplate();


    //I TRIED THIS ONE TOO AND IT GIVES AN EXCEPTION AS WELL
    //restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
    ResponseEntity<String> response = null;
    try {
    response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,
            String.class);
    } catch (Exception e) {
        Log.e("get post", e.getMessage(), e);
    }

    JSONObject motdsJSONObject = new JSONObject( response.getBody());

我收到了错误:

11-28 15:03:41.419: E/get post(7954): null
11-28 15:03:41.419: E/get post(7954): java.lang.NullPointerException
11-28 15:03:41.419: E/get post(7954):   at org.springframework.http.client.SimpleClientHttpResponse.getStatusCode(SimpleClientHttpResponse.java:62)
11-28 15:03:41.419: E/get post(7954):   at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
11-28 15:03:41.419: E/get post(7954):   at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:476)
11-28 15:03:41.419: E/get post(7954):   at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:438)
11-28 15:03:41.419: E/get post(7954):   at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:414)
11-28 15:03:41.419: E/get post(7954):   at com.mytemp.myapp.TimeSlipAPI.readTimeSlipFromAPI(TimeSlipAPI.java:75)
11-28 15:03:41.419: E/get post(7954):   at com.mytemp.myapp.TimeSlipPicker.populateList(TimeSlipPicker.java:382)
11-28 15:03:41.419: E/get post(7954):   at com.mytemp.myapp.TimeSlipPicker.onCreate(TimeSlipPicker.java:79)
11-28 15:03:41.419: E/get post(7954):   at android.app.Activity.performCreate(Activity.java:5104)
11-28 15:03:41.419: E/get post(7954):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-28 15:03:41.419: E/get post(7954):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-28 15:03:41.419: E/get post(7954):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-28 15:03:41.419: E/get post(7954):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 15:03:41.419: E/get post(7954):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-28 15:03:41.419: E/get post(7954):   at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 15:03:41.419: E/get post(7954):   at android.os.Looper.loop(Looper.java:137)
11-28 15:03:41.419: E/get post(7954):   at android.app.ActivityThread.main(ActivityThread.java:5039)
11-28 15:03:41.419: E/get post(7954):   at java.lang.reflect.Method.invokeNative(Native Method)
11-28 15:03:41.419: E/get post(7954):   at java.lang.reflect.Method.invoke(Method.java:511)
11-28 15:03:41.419: E/get post(7954):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-28 15:03:41.419: E/get post(7954):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-28 15:03:41.419: E/get post(7954):   at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案