1

I want to retrive data from server...This is my method to call

private void getBooks(){
    //While the app fetched data we are displaying a progress dialog

    //Creating a rest adapter
    RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(ROOT_URL)
            .build();

    //Creating an object of our api interface
    IApiMethods api = adapter.create(IApiMethods.class);

    api.getBooks("78", new Callback<JSONObject>() {
        @Override
        public void success(JSONObject jsonObject, Response response) {
            Toast.makeText(getBaseContext(),jsonObject.toString(),Toast.LENGTH_LONG).show();
            Log.e("response",jsonObject.toString());
        }

        @Override
        public void failure(RetrofitError error) {

            Log.e("error",error.getMessage());

        }
    });



}

This is my interface

     public interface IApiMethods {

      @FormUrlEncoded
      @POST("/product_info.php")
      public void getBooks(@Field("cid") String cid, Callback<JSONObject> jsonObjectCallback);

   }

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

Please help me..For the first time i am using retofit library.

user6076314
  • 189
  • 2
  • 11

1 Answers1

2

Your server is returning something like "your string from the server" instead of returning a JSON object:

{prop1: val1, prop2: val2, ...}

Either you need to change the response of you server to send back a JSON object or you need to change your interface definition to expect to String:

public interface IApiMethods {

  @FormUrlEncoded
  @POST("/product_info.php")
  public void getBooks(@Field("cid") String cid, Callback<String> jsonObjectCallback);

}
Stéphane
  • 1,075
  • 12
  • 18