1

I've read and tried just about every solution on the internet for doing this, and they all seem to produce an exception for me. I am successfully doing a GET but can't seem to get a POST to work.

Here's my current code:

    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());

There error that I'm getting:

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)
4

0 回答 0