1
+ package com .vogella .android .retrofitstackoverflow ;
2
+
3
+ import android .app .Activity ;
4
+ import android .app .ListActivity ;
5
+ import android .os .Bundle ;
6
+ import android .view .Menu ;
7
+ import android .view .MenuItem ;
8
+ import android .view .Window ;
9
+ import android .widget .ArrayAdapter ;
10
+ import android .widget .Toast ;
11
+
12
+ import com .google .gson .Gson ;
13
+ import com .google .gson .GsonBuilder ;
14
+
15
+ import java .util .ArrayList ;
16
+
17
+ import retrofit2 .Call ;
18
+ import retrofit2 .Callback ;
19
+ import retrofit2 .Response ;
20
+ import retrofit2 .Retrofit ;
21
+ import retrofit2 .converter .gson .GsonConverterFactory ;
22
+
23
+ public class MainActivity extends ListActivity implements Callback <StackOverflowQuestions > {
24
+
25
+ protected void onCreate (Bundle savedInstanceState ) {
26
+ super .onCreate (savedInstanceState );
27
+ requestWindowFeature (Window .FEATURE_INDETERMINATE_PROGRESS );
28
+ requestWindowFeature (Window .FEATURE_PROGRESS );
29
+ ArrayAdapter <Question > arrayAdapter =
30
+ new ArrayAdapter <Question >(this ,
31
+ android .R .layout .simple_list_item_1 ,
32
+ android .R .id .text1 ,
33
+ new ArrayList <Question >());
34
+ setListAdapter (arrayAdapter );
35
+ setProgressBarIndeterminateVisibility (true );
36
+ setProgressBarVisibility (true );
37
+ }
38
+
39
+ @ Override
40
+ public boolean onCreateOptionsMenu (Menu menu ) {
41
+ getMenuInflater ().inflate (R .menu .main_menu , menu );
42
+ return true ;
43
+ }
44
+
45
+ @ Override
46
+ public boolean onOptionsItemSelected (MenuItem item ) {
47
+ setProgressBarIndeterminateVisibility (true );
48
+ Gson gson = new GsonBuilder ()
49
+ .setDateFormat ("yyyy-MM-dd'T'HH:mm:ssZ" )
50
+ .create ();
51
+ Retrofit retrofit = new Retrofit .Builder ()
52
+ .baseUrl ("https://api.stackexchange.com" )
53
+ .addConverterFactory (GsonConverterFactory .create (gson ))
54
+ .build ();
55
+
56
+ // prepare call in Retrofit 2.0
57
+ StackOverflowAPI stackOverflowAPI = retrofit .create (StackOverflowAPI .class );
58
+
59
+ Call <StackOverflowQuestions > call = stackOverflowAPI .loadQuestions ("android" );
60
+ //asynchronous call
61
+ call .enqueue (this );
62
+
63
+ // synchronous call would be with execute, in this case you
64
+ // would have to perform this outside the main thread
65
+ // call.execute()
66
+
67
+ // to cancel a running request
68
+ // call.cancel();
69
+ // calls can only be used once but you can easily clone them
70
+ //Call<StackOverflowQuestions> c = call.clone();
71
+ //c.enqueue(this);
72
+
73
+ return true ;
74
+ }
75
+
76
+
77
+ @ Override
78
+ public void onResponse (Call <StackOverflowQuestions > call , Response <StackOverflowQuestions > response ) {
79
+ setProgressBarIndeterminateVisibility (false );
80
+ ArrayAdapter <Question > adapter = (ArrayAdapter <Question >) getListAdapter ();
81
+ adapter .clear ();
82
+ adapter .addAll (response .body ().items );
83
+ }
84
+
85
+ @ Override
86
+ public void onFailure (Call <StackOverflowQuestions > call , Throwable t ) {
87
+ Toast .makeText (MainActivity .this , t .getLocalizedMessage (), Toast .LENGTH_SHORT ).show ();
88
+ }
89
+ }
0 commit comments