Upgrade: sending POST request to server in android

I use Retrofit to call the API. I am sending an API mail request, but in the callback I get empty JSON, like this {}.

Below is the RetrofitService code

@POST("/com/searchusers.php") void getUser(@Body JSONObject searchstring, Callback<JSONObject> callBack); 

where searchstring JSON is like this {"search": "nitesh"}. In response, I should get the details of the user "nitesh".

Below is the code to send a POST request

 RetrofitService mRetrofitService = app.getRetrofitService(); mRetrofitService.getUser(user, new Callback<JSONObject>() { @Override public void success(JSONObject result, Response arg1) { System.out.println("success, result: " + result); } @Override public void failure(RetrofitError error) { System.out.println("failure, error: " + error); } }); 

I get this conclusion success, result: {}

Expected Result: success, result: {"name": "nitesh", .... the rest of the details}

Edit: I tried using Response instead of JSONObject, like this CallBack<Response> , and then I converted the original response to String and got the expected result. But the problem is that in String I want to get the answer in JSONObject.

How to get the exact result using CallBack<JSONObject> ...?

+6
json android post retrofit
May 25 '14 at 8:49
source share
2 answers

After waiting for a better answer, I thought about how to answer my question. This is how I solved my problem.

I changed the converter RestAdapter of RetrofitService and created my own converter. Below is my StringConverter

 static class StringConverter implements Converter { @Override public Object fromBody(TypedInput typedInput, Type type) throws ConversionException { String text = null; try { text = fromStream(typedInput.in()); } catch (IOException ignored) {/*NOP*/ } return text; } @Override public TypedOutput toBody(Object o) { return null; } public static String fromStream(InputStream in) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder out = new StringBuilder(); String newLine = System.getProperty("line.separator"); String line; while ((line = reader.readLine()) != null) { out.append(line); out.append(newLine); } return out.toString(); } } 

Then I install this converter in the RestAdapter in the Application class.

 RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(BASE_URL) .setConverter(new StringConverter()) .build(); mRetrofitService = restAdapter.create(RetrofitService.class); 

Now when I use Retrofit, I get the answer in String. Then I converted this String to JSONObject.

 RetrofitService mRetrofitService = app.getRetrofitService(); mRetrofitService.getUser(user, new Callback<String>() { @Override public void success(String result, Response arg1) { System.out.println("success, result: " + result); JSONObject jsonObject = new JSONObject(result); } @Override public void failure(RetrofitError error) { System.out.println("failure, error: " + error); } }); 

Therefore, I got the result in the form of JSON. Then I parsed this JSON as needed.

+8
Jul 03 '14 at 10:16
source share
— -

it can help you

 JSONObject responseJsonObj = new JSONObject(responseString); 
-3
Jun 27 '14 at 12:56 on
source share



All Articles