Question

I have some problems using retrofit as my web communication interface against a php webservice contained in a worpress website - upon a call for one of the JSON API methods in the WP site I get an SSL exception on my android client even though I run over http and not https.

Here is my code -

public class RestApi {
    private static final String API_URL = "https://tmc.co.nf/api";
    private SmokeTalkRest service;
    interface SmokeTalkRest {

        @FormUrlEncoded
        @POST("/get_nonce")
        void getNonce(@Field("controller") String controller, @Field("method") String method, Callback<String> callback);
    }

    public RestApi() {

        // Create a very simple REST adapter which points the GitHub API
        // endpoint.
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setServer(API_URL).build();

        // Create an instance of our GitHub API interface.
        service = restAdapter.create(SmokeTalkRest.class);
    }

    public void getNonceForMethod(Method method, Callback<String> callback) {
        service.getNonce("user", method.name(), callback);
    }
}

The get nonce is called upon a button click, did someone already bumped into that?

Was it helpful?

Solution

I believe the issue you are having is your trying to call retrofit but not using the async version. The callback is probably the easiest to use.

@GET("/user/{id}")  
void listUser(@Path("id") int id, Callback<User> cb);

RestAdapter restAdapter = new RestAdapter.Builder()
            .setServer("baseURL")     
            .build();
ClientInterface service = restAdapter.create(ClientInterface.class);

Callback callback = new Callback() {
    @Override
    public void success(Object o, Response response) {
//do something
    }

    @Override
    public void failure(RetrofitError retrofitError) {

    }
};
service.listUser(1, callback);

How to implement an async Callback using Square's Retrofit networking library

Android now requires you to do any webrequests async otherwise it will error out.

Also, retorfit will convert/parse the object for you so you dont have to. It saves time when it comes to creating async tasks and setting up the parsing. It also give a nice standard to go by when doing requests as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top