Skip to content

Commit 9ce05ad

Browse files
author
Rajeev kumar
committed
Retrofit library sample code by tutorialwing.com
0 parents  commit 9ce05ad

40 files changed

+836
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures

.idea/compiler.xml

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/copyright/profiles_settings.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dictionaries/Rajeevkumar.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+89
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.3"
6+
7+
defaultConfig {
8+
applicationId "tutorialwing.com.retrofitlibrary"
9+
minSdkVersion 16
10+
targetSdkVersion 23
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
testCompile 'junit:junit:4.12'
25+
compile 'com.android.support:appcompat-v7:23.3.0'
26+
compile 'com.squareup.retrofit2:retrofit:2.0.2'
27+
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
28+
29+
compile 'com.android.support:recyclerview-v7:23.3.0'
30+
}

app/proguard-rules.pro

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/Rajeevkumar/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package tutorialwing.com.retrofitlibrary;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}

app/src/main/AndroidManifest.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest package="tutorialwing.com.retrofitlibrary"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity android:name=".activity.MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN"/>
16+
17+
<category android:name="android.intent.category.LAUNCHER"/>
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package tutorialwing.com.retrofitlibrary.activity;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.support.v7.widget.LinearLayoutManager;
6+
import android.support.v7.widget.RecyclerView;
7+
import android.util.Log;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import retrofit2.Call;
13+
import retrofit2.Callback;
14+
import retrofit2.Response;
15+
import tutorialwing.com.retrofitlibrary.R;
16+
import tutorialwing.com.retrofitlibrary.adapter.QuestionAdapter;
17+
import tutorialwing.com.retrofitlibrary.model.Question;
18+
import tutorialwing.com.retrofitlibrary.model.QuestionList;
19+
import tutorialwing.com.retrofitlibrary.rest.QuestionAPIService;
20+
import tutorialwing.com.retrofitlibrary.rest.RestClient;
21+
22+
public class MainActivity extends AppCompatActivity {
23+
24+
private static final String TAG = MainActivity.class.getSimpleName();
25+
26+
QuestionAPIService apiService;
27+
28+
RecyclerView recyclerView;
29+
QuestionAdapter adapter;
30+
List<Question> questions = new ArrayList<>();
31+
32+
@Override
33+
protected void onCreate(Bundle savedInstanceState) {
34+
super.onCreate(savedInstanceState);
35+
setContentView(R.layout.activity_main);
36+
37+
apiService = RestClient.getClient().create(QuestionAPIService.class);
38+
39+
recyclerView = (RecyclerView) findViewById(R.id.questionListRecyclerView);
40+
recyclerView.setLayoutManager(new LinearLayoutManager(this));
41+
42+
adapter = new QuestionAdapter(questions, R.layout.question_item, getApplicationContext());
43+
recyclerView.setAdapter(adapter);
44+
45+
fetchQuetionList();
46+
}
47+
48+
private void fetchQuetionList() {
49+
Call<QuestionList> call = apiService.fetchQuestions("android");
50+
call.enqueue(new Callback<QuestionList>() {
51+
@Override
52+
public void onResponse(Call<QuestionList> call, Response<QuestionList> response) {
53+
Log.d(TAG, "Total number of questions fetched : " + response.body().getQuestions().size());
54+
55+
questions.addAll(response.body().getQuestions());
56+
adapter.notifyDataSetChanged();
57+
}
58+
59+
@Override
60+
public void onFailure(Call<QuestionList> call, Throwable t) {
61+
Log.e(TAG, "Got error : " + t.getLocalizedMessage());
62+
}
63+
});
64+
}
65+
}
66+
67+
68+
69+

0 commit comments

Comments
 (0)