1

I am using Retrofit which uses GSON for parsing.The server will check the parameter i am sending and returns two responses Accordingly. If the parameter is valid i am getting the following response

[
  true
]

If it is not valid then i get response as,

   [
      "Sorry, <em class=\"placeholder\">[email protected]</em> is not recognized as a user name or an e-mail address."
    ]

This is the call method i am using.

@Override

public void onResponse(Call<String> call, Response<String> response) {
mProgressBar.setVisibility(View.GONE);
    Log.d("Response ", ""+response);


}

here response.body giving me as null. But there is a response.which is viewable in OKHttp Log.

1
  • Make that parameter as Object. After parsing check if the parsed Object instanceof Boolean or String. Commented Aug 29, 2016 at 11:48

4 Answers 4

1

you can try this

 JSONArray jArray=new JSONArray(yourString);

 String str=jArray.getString(0);;
 if(str.equalsIgnoreCase("true")
 {
      //your code
 }else
 {
 }
Sign up to request clarification or add additional context in comments.

6 Comments

i am not getting as jsonob.
i am not even getting as string
the value i am getting response.body() is null in my activity.But there is a response which we can see inD/OkHttp: ["Sorry, <em class=\"placeholder\">[email protected]</em> is not recognized as a user name or an e-mail address."]
first convert it into string than try it .
Thats what i am trying !! Thats actually my question
|
0
JSONArray jarray = new JSONObject(jsonString);
for (int i = 0; i < jarray.length(); i++) {
      String string = jarray.getString(i);
      log.e("String",string);
}

that's working great. Please try this.

6 Comments

I am not even getting JSONObject
but you can get responce in onsuccess method of retrofit. In this on siccess method you can parsing directly.
i am getting null as response.body()
first you check your url. if url is right check your retrofit code.
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { String sresponce = response.body(); }
|
0

You can use JsonParser provided by Gson. It will parse the json input to dynamic JsonElement.

Look at MByD example:

 public String parse(String jsonLine) {
    JsonElement jelement = new JsonParser().parse(jsonLine);
    JsonObject  jobject = jelement.getAsJsonObject();
    jobject = jobject.getAsJsonObject("data");
    JsonArray jarray = jobject.getAsJsonArray("translations");
    jobject = jarray.get(0).getAsJsonObject();
    String result = jobject.get("translatedText").toString();
    return result;
}

Comments

0

since the server return diffrent codes

can you try

Call<Void> 

and check the response code this might work for now until you know what is wrong void will not parse any thing so it shouldn't cause a crash

///////

did you try

Call<ArrayList<String>>

also are you sure the server returns 200 in both cases ?

2 Comments

I tried this one first.that time i got null pointer exception. the server returns 200 and 406 respectively different in two cases.
try Call<Void> and tell me how did it go

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.