0

I would like to know if there's any way to extract a Json string from a http response.body(). Inside my response.body() I have: {"er":"manualBlock"} and I would like to deal with this string without having to use the split method.

Edit I have this so far:

String[] parts = response.body().string().split("-"); 
        result = parts[0]; 

if (result != null && result.equals("{\"er\":\"manualBlock\"}")) {
          throw new BlockeduserException("User blocked", null);
        }
5
  • 1
    What HTTP client do you use? Commented Aug 22, 2017 at 10:28
  • Hi, thanks for the answer, I am using Retrofit. Commented Aug 22, 2017 at 10:34
  • Also, could you please paste the code you currently have? Commented Aug 22, 2017 at 10:37
  • implement toString method in your Response class and do like this response.body.toString() Commented Aug 22, 2017 at 10:39
  • I just added the code that I have so far. Commented Aug 22, 2017 at 10:54

1 Answer 1

1

I managed to solve my problem by creating a class like this:

public class BlockResponse {

    public String er;
}

And then I used the google-Gson to handle everything by doing this:

String serverResponse = response.body().string();
Gson gson = new Gson();
result = gson.fromJson(serverResponse, BlockResponse.class);

And for the comparison I used:

if (result != null && result.er.equals("manualBlock")) {
    throw new BlockeduserException("User blocked", null);
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.