Skip to content

Commit 4cb112f

Browse files
author
David Weiser
committed
adds Android Retrofit Github example
1 parent ae69e4c commit 4cb112f

28 files changed

Lines changed: 717 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
6+
defaultConfig {
7+
applicationId "com.vogella.android.retrofitgithubcomment"
8+
minSdkVersion 15
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
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+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
exclude group: 'com.android.support', module: 'support-annotations'
26+
})
27+
compile 'com.android.support:appcompat-v7:25.1.1'
28+
compile 'com.squareup.retrofit2:retrofit:2.1.0'
29+
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
30+
compile 'com.google.code.gson:gson:2.6.1'
31+
testCompile 'junit:junit:4.12'
32+
}
Lines changed: 17 additions & 0 deletions
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 /home/david/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,26 @@
1+
package com.vogella.android.retrofitgithubcomment;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.vogella.android.retrofitgithubcomment", appContext.getPackageName());
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.retrofitgithubcomment">
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=".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>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.vogella.android.retrofitgithubcomment;
2+
3+
import java.util.List;
4+
5+
import okhttp3.ResponseBody;
6+
import retrofit2.Call;
7+
import retrofit2.http.Body;
8+
import retrofit2.http.GET;
9+
import retrofit2.http.Header;
10+
import retrofit2.http.POST;
11+
import retrofit2.http.Url;
12+
13+
14+
public interface GithubAPI {
15+
String ENDPOINT = "https://api.github.com";
16+
17+
@GET("/issues")
18+
Call<List<GithubIssue>> getIssues();
19+
20+
@POST
21+
Call<ResponseBody> postComment(@Url String url, @Body GithubIssue issue);
22+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.vogella.android.retrofitgithubcomment;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
/**
6+
* Created by david on 20.02.17.
7+
*/
8+
9+
public class GithubIssue {
10+
11+
String id;
12+
String title;
13+
String comments_url;
14+
15+
@SerializedName("body")
16+
String comment;
17+
18+
public String getId() {
19+
return id;
20+
}
21+
22+
public void setId(String id) {
23+
this.id = id;
24+
}
25+
26+
public String getTitle() {
27+
return title;
28+
}
29+
30+
public void setTitle(String title) {
31+
this.title = title;
32+
}
33+
34+
public String getCommentsUrl() {
35+
return comments_url;
36+
}
37+
38+
public void setCommentsUrl(String commentsUrl) {
39+
this.comments_url = commentsUrl;
40+
}
41+
42+
public String getComment() {
43+
return comment;
44+
}
45+
46+
public void setComment(String comment) {
47+
this.comment = comment;
48+
}
49+
50+
51+
@Override
52+
public String toString() {
53+
return id + " - " + title;
54+
}
55+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.vogella.android.retrofitgithubcomment;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.util.Log;
6+
import android.view.View;
7+
import android.widget.ArrayAdapter;
8+
import android.widget.Button;
9+
import android.widget.EditText;
10+
import android.widget.Spinner;
11+
import android.widget.Toast;
12+
13+
import com.google.gson.Gson;
14+
import com.google.gson.GsonBuilder;
15+
16+
import java.io.IOException;
17+
import java.util.List;
18+
19+
import okhttp3.Credentials;
20+
import okhttp3.Interceptor;
21+
import okhttp3.OkHttpClient;
22+
import okhttp3.Request;
23+
import okhttp3.ResponseBody;
24+
import retrofit2.Call;
25+
import retrofit2.Callback;
26+
import retrofit2.Response;
27+
import retrofit2.Retrofit;
28+
import retrofit2.converter.gson.GsonConverterFactory;
29+
30+
public class MainActivity extends AppCompatActivity {
31+
32+
GithubAPI githubAPI;
33+
String credentials = Credentials.basic("aUsername", "aPassword");
34+
35+
Spinner issuesIdSpinner;
36+
EditText commentEditText;
37+
Button sendButton;
38+
39+
@Override
40+
protected void onCreate(Bundle savedInstanceState) {
41+
super.onCreate(savedInstanceState);
42+
setContentView(R.layout.activity_main);
43+
44+
sendButton = (Button) findViewById(R.id.send_comment_button);
45+
46+
issuesIdSpinner = (Spinner) findViewById(R.id.issues_spinner);
47+
issuesIdSpinner.setEnabled(false);
48+
49+
commentEditText = (EditText) findViewById(R.id.comment_edittext);
50+
51+
createRetrofitClient();
52+
}
53+
54+
private void createRetrofitClient() {
55+
Gson gson = new GsonBuilder()
56+
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
57+
.create();
58+
59+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
60+
.addInterceptor(new Interceptor() {
61+
@Override
62+
public okhttp3.Response intercept(Chain chain) throws IOException {
63+
Request originalRequest = chain.request();
64+
65+
Request.Builder builder = originalRequest.newBuilder().header("Authorization",
66+
credentials);
67+
68+
Request newRequest = builder.build();
69+
return chain.proceed(newRequest);
70+
}
71+
}).build();
72+
73+
Retrofit retrofit = new Retrofit.Builder()
74+
.baseUrl(GithubAPI.ENDPOINT)
75+
.client(okHttpClient)
76+
.addConverterFactory(GsonConverterFactory.create(gson))
77+
.build();
78+
79+
githubAPI = retrofit.create(GithubAPI.class);
80+
}
81+
82+
public void onClick(View view) {
83+
84+
85+
switch (view.getId()) {
86+
case R.id.loadIssues_button:
87+
Call<List<GithubIssue>> callIssues = githubAPI.getIssues();
88+
callIssues.enqueue(issues);
89+
break;
90+
case R.id.send_comment_button:
91+
String newComment = commentEditText.getText().toString();
92+
if (!newComment.isEmpty()) {
93+
GithubIssue selectedItem = (GithubIssue) issuesIdSpinner.getSelectedItem();
94+
selectedItem.setComment(newComment);
95+
Call<ResponseBody> postComment = githubAPI.postComment(selectedItem.getCommentsUrl(), selectedItem);
96+
postComment.enqueue(comment);
97+
} else {
98+
Toast.makeText(MainActivity.this, "Please enter a comment", Toast.LENGTH_LONG).show();
99+
}
100+
break;
101+
}
102+
}
103+
104+
Callback<List<GithubIssue>> issues = new Callback<List<GithubIssue>>() {
105+
@Override
106+
public void onResponse(Call<List<GithubIssue>> call, Response<List<GithubIssue>> response) {
107+
if (response.isSuccessful()) {
108+
List<GithubIssue> issuesList = response.body();
109+
GithubIssue[] idArray = issuesList.toArray(new GithubIssue[issuesList.size()]);
110+
ArrayAdapter<GithubIssue> spinnerAdapter = new ArrayAdapter<GithubIssue>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, idArray);
111+
issuesIdSpinner.setAdapter(spinnerAdapter);
112+
113+
issuesIdSpinner.setEnabled(true);
114+
commentEditText.setEnabled(true);
115+
sendButton.setEnabled(true);
116+
} else {
117+
Log.e("onResponse failure", "Code: " + response.code() + " , Message: " + response.message());
118+
}
119+
}
120+
121+
@Override
122+
public void onFailure(Call<List<GithubIssue>> call, Throwable t) {
123+
t.printStackTrace();
124+
}
125+
};
126+
127+
Callback<ResponseBody> comment = new Callback<ResponseBody>() {
128+
@Override
129+
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
130+
if (response.isSuccessful()) {
131+
commentEditText.setText("");
132+
Toast.makeText(MainActivity.this, "Comment created", Toast.LENGTH_LONG).show();
133+
} else {
134+
Log.e("onResponse failure", "Code: " + response.code() + " , Message: " + response.message());
135+
}
136+
}
137+
138+
@Override
139+
public void onFailure(Call<ResponseBody> call, Throwable t) {
140+
t.printStackTrace();
141+
}
142+
};
143+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/activity_main"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:paddingBottom="@dimen/activity_vertical_margin"
8+
android:paddingLeft="@dimen/activity_horizontal_margin"
9+
android:paddingRight="@dimen/activity_horizontal_margin"
10+
android:paddingTop="@dimen/activity_vertical_margin"
11+
tools:context="com.vogella.android.retrofitgithubcomment.MainActivity">
12+
13+
<Spinner
14+
android:id="@+id/issues_spinner"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"/>
17+
18+
<EditText
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:id="@+id/comment_edittext"
22+
android:layout_below="@id/issues_spinner"
23+
android:enabled="false"
24+
android:maxLines="1"
25+
android:inputType="text"
26+
android:imeOptions="actionDone"
27+
android:hint="Your comment" />
28+
29+
<Button
30+
android:id="@+id/loadIssues_button"
31+
android:layout_width="match_parent"
32+
android:layout_height="wrap_content"
33+
android:layout_alignParentBottom="true"
34+
android:gravity="center"
35+
android:onClick="onClick"
36+
android:text="Load issues" />
37+
<Button
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content"
40+
android:text="Send comment"
41+
android:layout_above="@id/loadIssues_button"
42+
android:onClick="onClick"
43+
android:enabled="false"
44+
android:id="@+id/send_comment_button"/>
45+
</RelativeLayout>

0 commit comments

Comments
 (0)