David Gordon
David Gordon

Reputation: 287

Retrofit error android

I've always been curious about implementing Retrofit in my applications to make simple POST and GET requests. However I have have problems wrapping my head around it. Today I decided to try to integrate it in my app but am having troubles. If you could have a look at my code that would be great. This is the error I'm getting

"Caused by: retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2"

This is the api call if you want to try it out. http://api.thebookofeveryone.com/social/makelove?phrase=love

I also know I'm not doing anything with the returned Image Object right now as I need to solve this problem first.

Thank you.

MainActivity

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    extraThread task = new extraThread();
    task.execute();

}


public class extraThread extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        RetrofitInterface.getImageApiClient().getImage("love");
        return null;
    }

}

RetroFit Interface

public class RetrofitInterface {

    private static ImageApiInterface sImageService;

    public static ImageApiInterface getImageApiClient() {
        if (sImageService == null) {
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint("http://api.thebookofeveryone.com")
                    .build();

            sImageService = restAdapter.create(ImageApiInterface.class);
        }

        return sImageService;
    }

    public interface ImageApiInterface {
        @GET("/social/makelove")
        Image getImage(@Query("phrase") String phrase);
    }


}

Image Class

public class Image {
    boolean success;
    String url;
}

Upvotes: 1

Views: 845

Answers (1)

pt2121
pt2121

Reputation: 11870

It's a JSON parsing error. Change you Image class to:

public class Image {

    private Boolean success;
    private String url;

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}

Or:

public class Image {

    Boolean success;
    String url;

}

Everything should work fine.

To create a POJO from JSON or JSON-Schema, I recommend http://www.jsonschema2pojo.org/. It's really helpful.

Upvotes: 1

Related Questions