Is there a greate sample using the retrofit

184 Views Asked by At

I'm going to develop a small-sized system to raise my developing skills.

It consists of the three parts listed below: 1. Web DB 2. Web Page 3. Android App

The main feature is managing the members. (login, just showing the user information)

At this point, I'm wondering about the android app part. Especially, the HTTP.

I found two libraries which are JSoup and Retrofit. As far as I can tell, those libraries are a little bit different.

I think the retrofit is a better fit for me... Up until now I couldn't find a good sample...

Can you give me a hint how to do this?

2

There are 2 best solutions below

0
On

Retrofit example

Synchronous

Interface

public interface RestApi {

    @POST("/Login")
    String login(@Body User user);
}

Simple class which follows the singleton design pattern

public class RestClient {

    private static RestApi API_INSTANCE;
    private static final String ENDPOINT = "http://192.168.6.99:50500/Phone";


    static {
        setUpHttpClient();
    }

    private static void setUpHttpClient() {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(ENDPOINT)
                .setLogLevel(BuildConfig.DEBUG ? 
                RestAdapter.LogLevel.FULL :    
                RestAdapter.LogLevel.NONE)
                .build();
        API_INSTANCE = restAdapter.create(RestApi.class);
    }

    private RestClient() {}

    public static RestApi getApiInstance() {
        return API_INSTANCE;
    }

}

Call it for example in an IntentService

String userToken = "";
        try {
            // post to http://192.168.6.99:50500/Phone/Login
            userToken = RestClient.getApiInstance().login(new User(login, password));
        } catch (RetrofitError error) {
            //...
        }

build.gradle

compile 'com.squareup.retrofit:retrofit:1.9.0'

Or Asynchronous

interface

public interface RestApi {

    @POST("/Login")
    void login(@Body User user, Callback<String> token);
}

and do the request with a callback..

0
On

If you are trying to connect to a Web Database I would suggest using Volley which is really simple and straightforward and really powerful yet: https://developer.android.com/training/volley/index.html. Here's an example on how you could set your query with volley from the android developer site:

final TextView mTextView = (TextView) findViewById(R.id.text);
...

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
@Override
public void onResponse(String response) {
    // Display the first 500 characters of the response string.
    mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
    mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);