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\">names@gmail.inp</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.

Community
  • 1
  • 1
Noufal
  • 439
  • 1
  • 13
  • 35

4 Answers4

1

you can try this

 JSONArray jArray=new JSONArray(yourString);

 String str=jArray.getString(0);;
 if(str.equalsIgnoreCase("true")
 {
      //your code
 }else
 {
 }
h_patel
  • 744
  • 5
  • 16
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.

Hardik Vaghasiya
  • 298
  • 1
  • 3
  • 11
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;
}
Community
  • 1
  • 1
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
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 ?

Hala.M
  • 766
  • 6
  • 15