Last active
February 3, 2016 16:05
-
-
Save Khanashima/155646a07d2da3666f61 to your computer and use it in GitHub Desktop.
Retrofit ver2.0.0の使い方 ref: http://qiita.com/kiimiiis/items/9e1078c66646c02cc103
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3' | |
compile 'com.squareup.retrofit2:converter-gson:latest.release' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compile 'com.google.code.gson:gson:2.3.1' | |
compile 'org.glassfish:javax.annotation:10.0-b28' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//QiitaApiの使用 | |
Retrofit retrofit = new Retrofit.Builder() | |
.baseUrl("https://qiita.com") | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
QiitaApiService qiitaApiservice = retrofit.create(QiitaApiService.class); | |
Call<List<QiitaGsonResponse>> call = qiitaApiservice.getData("1", "20", "android"); | |
try { | |
call.enqueue(new Callback<List<QiitaGsonResponse>>() { | |
@Override | |
public void onResponse(Response<List<QiitaGsonResponse>> response) { | |
String title = response.body().get(0).getTitle(); | |
Log.d("Qiita",title); | |
} | |
@Override | |
public void onFailure(Throwable t) { | |
Log.d("Qiita", "error"); | |
} | |
}); | |
} catch (Exception e) { | |
Log.d("Qiita", "レスポンスエラー"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by hanasjima on 2016/01/28. | |
*/ | |
public interface QiitaApiService { | |
@GET("/api/v2/items") | |
Call<List<QiitaGsonResponse>> getData(@Query("page") String page, @Query ("per_page") String per_page, @Query("query") String query); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment