How to use Retrofit library

Retrofit parse JSON of your web service. Use Retrofit library is very simple

Retrofit turns your REST API into a Java interface.

you can download this example from here

You need these librarires.

JSON

Then you can get your JSON, We use this chrome app. Now you can build your model from JSON in this web

You create a new class which name is RestClient

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.thedeveloperworldisyours.simpleretrofic.webservice;
 
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.OkClient;
import retrofit.client.Response;
import retrofit.http.GET;
import retrofit.http.Query;
import android.os.Handler;
import android.util.Log;
 
import com.squareup.okhttp.OkHttpClient;
import com.thedeveloperworldisyours.simpleretrofic.model.Question;
import com.thedeveloperworldisyours.simpleretrofic.utils.Constants;
 
public class RestClient {
 
    public interface ClientInterface{
          @GET("/questions")
          void getQuestions( @Query("sort") String sort, @Query("site") String site,@Query("pagesize") String pagesize,@Query("page") String page, Callback<Question> callback);
        }
 
    public static ClientInterface initRestAdapter()
    {
        OkHttpClient client = new OkHttpClient();
 
        return (ClientInterface) new RestAdapter.Builder()
                .setClient(new OkClient(client))
                .setEndpoint(Constants.URL)
                .build()
                .create(ClientInterface.class);
    }
    public static void GetQuestions(final Handler mHandler) {
        Callback<Question> callback = new Callback<Question>() {
 
            @Override
            public void failure(RetrofitError resp) {
                Log.v("failure", String.valueOf(resp.getMessage()));
            }
 
            @Override
            public void success(Question info, Response resp) {
                Log.v("success", String.valueOf(resp.getStatus()));
            }
        };
        RestClient.initRestAdapter().getQuestions(Constants.SORT, Constants.SITE,
                Constants.PAGESIZE, Constants.PAGE, callback);
    }
 
}

Now in your MainActivity put this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.thedeveloperworldisyours.simpleretrofic;
 
import java.util.ArrayList;
import java.util.List;
 
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
 
import com.thedeveloperworldisyours.simpleretrofic.model.Question;
import com.thedeveloperworldisyours.simpleretrofic.utils.Constants;
import com.thedeveloperworldisyours.simpleretrofic.utils.Utils;
import com.thedeveloperworldisyours.simpleretrofic.webservice.RestClient;
 
public class MainActivity extends ActionBarActivity {
 
    List<String> mListTitle = new ArrayList<String>();
    private ListView mListView;
 
        protected ListView getListView() {
            if (mListView == null) {
                mListView = (ListView) findViewById(android.R.id.list);
            }
            return mListView;
        }
 
        protected void setListAdapter(ListAdapter adapter) {
            getListView().setAdapter(adapter);
        }
 
        protected ListAdapter getListAdapter() {
            ListAdapter adapter = getListView().getAdapter();
            if (adapter instanceof HeaderViewListAdapter) {
                return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
            } else {
                return adapter;
            }
        }
 
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);
        getQuestionCheckInternet();
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
 
        switch (item.getItemId())
        {
        case R.id.action_refresh:
            refreshList();
            if(Utils.isOnline(MainActivity.this)){
                Toast.makeText(this, R.string.action_refresh, Toast.LENGTH_SHORT).show();
            }
            return true;
 
        default:
            return super.onOptionsItemSelected(item);
        }
        }
    public void refreshList(){
        mListTitle.clear();
        getQuestionCheckInternet();
        createList();
 
    }
 
    public void getQuestionCheckInternet(){
        if(Utils.isOnline(MainActivity.this)){
            getQuestions();
        }else{
            Toast.makeText(this, R.string.info_offline, Toast.LENGTH_LONG).show();
        }
    }
 
    protected void onListItemClick(ListView lv, View v, int position, long id) {
        getListView().getOnItemClickListener().onItemClick(lv, v, position, id);
        String item = (String) getListAdapter().getItem(position);
        Toast.makeText(this, item, Toast.LENGTH_LONG).show();
    }
 
    public void getQuestions() {
        Callback<Question> callback = new Callback<Question>() {
 
            @Override
            public void failure(RetrofitError resp) {
                Log.v("failure", String.valueOf(resp.getMessage()));
            }
 
            @Override
            public void success(Question info, Response resp) {
                Log.v("success", String.valueOf(resp.getStatus()));
                getTitle(info);
                createList();
            }
        };
        RestClient.initRestAdapter().getQuestions(Constants.SORT, Constants.SITE,
                Constants.PAGESIZE, Constants.PAGE, callback);
    }
 
    public List<String> getTitle(Question info) {
        for (int i = 0; i < info.getItems().size(); i++) {
            mListTitle.add(info.getItems().get(i).getTitle());
        }
        return mListTitle;
    }
    public void createList(){
 
        // use your custom layout
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,mListTitle);
        setListAdapter(adapter);
    }
}

you can download this example from here

Leave a Reply

Your email address will not be published. Required fields are marked *