0

Data Post on Database. Its give Result Json Format.

{"response":"exist"}

When get Response using Retrofit Library and json Parsing using GSON .its goes on OnFailure method and its give error.

call.enqueue(new Callback<Register>() {
           @Override
           public void onResponse(Call<Register> call, Response<Register> response) {


               if (response.body().equals("ok")){

                   Toast.makeText(MainActivity.this, "Registration is Successfully", Toast.LENGTH_SHORT).show();

               }else if (response.body().equals("exiest")){

                   Toast.makeText(MainActivity.this, "Already Exiest", Toast.LENGTH_SHORT).show();

               }else if(response.body().equals("error")){

                   Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_SHORT).show();

               }
           }

           @Override
           public void onFailure(Call<Register> call, Throwable t) {

               Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();

           }
       });

Error is :

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

how it Resolved its Please Guide Me.

5
  • have you added addConverterFactory(GsonConverterFactory.create()) to your retrofit builder Commented Feb 27, 2019 at 9:29
  • post your Register model here Commented Feb 27, 2019 at 9:30
  • Gson gson = new GsonBuilder() .setLenient() .create(); retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); Commented Feb 27, 2019 at 10:45
  • @POST("Registration.php") Call<Register> registerUser(@Field("fname") String fName, @Field("mname") String mName, @Field("lname") String lName, @Field("mobile_no") String mobile, @Field("email_id") String email); Commented Feb 27, 2019 at 10:59
  • @SerializedName("response") @Expose public String response; public String getResponse() { return response; } public void setResponse(String response) { this.response = response; } Commented Feb 27, 2019 at 11:05

2 Answers 2

0

You are parsing your response wrong . and its exist , you are comparing exiest

change your parsing like this

if (response.body().getResponse().equals("ok")){

                   Toast.makeText(MainActivity.this, "Registration is Successfully", Toast.LENGTH_SHORT).show();

               }else if (response.body()..getResponse().equals("exist")){

                   Toast.makeText(MainActivity.this, "Already Exiest", Toast.LENGTH_SHORT).show();

               }else if(response.body().getResponse().equals("error")){

                   Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_SHORT).show();

               }
Sign up to request clarification or add additional context in comments.

Comments

0

You are parsing your Data wrong. Retrofit convert response in Register model class as given in Callback, you should read the value from this.

Register register= response.body();
if (register.getResponse().equals("ok")){
   Toast.makeText(MainActivity.this, "Registration is Successfully", Toast.LENGTH_SHORT).show();
 }else if (register.getResponse().equals("exist")){
   Toast.makeText(MainActivity.this, "Already Exist", Toast.LENGTH_SHORT).show();
 }else if(register.getResponse().equals("error")){
    Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_SHORT).show();
 }

Register.java

public class Register {
    @SerializedName("response")
    @Expose
    private String response;
    public String getResponse() {
        return response;
    }
    public void setResponse(String response) {
        this.response = response;
    }
}

4 Comments

Are you sure you receive proper JSON output in your response?
{"response":"exist"}
i have received above JSON output.
This error can't occur if you receive the above output. Check this answer stackoverflow.com/a/31425418/1546759

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.