Skip to content

Commit b448bef

Browse files
author
David Weiser
committed
updates retrofit github example
1 parent 00c3a9a commit b448bef

File tree

38 files changed

+535
-296
lines changed

38 files changed

+535
-296
lines changed

com.vogella.android.retrofitgithubcomment/app/build.gradle renamed to com.vogella.android.retrofitgithub/app/build.gradle

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ android {
44
compileSdkVersion 25
55
buildToolsVersion "25.0.2"
66
defaultConfig {
7-
applicationId "com.vogella.android.retrofitgithubcomment"
7+
applicationId "com.vogella.android.retrofitgithub"
88
minSdkVersion 15
99
targetSdkVersion 25
1010
versionCode 1
@@ -24,9 +24,11 @@ dependencies {
2424
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2525
exclude group: 'com.android.support', module: 'support-annotations'
2626
})
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.8.0'
27+
compile 'com.android.support:appcompat-v7:25.3.1'
28+
compile 'com.squareup.retrofit2:retrofit:2.3.0'
29+
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
30+
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
31+
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
32+
3133
testCompile 'junit:junit:4.12'
3234
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.vogella.android.retrofitgithubcomment;
1+
package com.vogella.android.retrofitgithub;
22

33
import android.content.Context;
44
import android.support.test.InstrumentationRegistry;
@@ -21,6 +21,6 @@ public void useAppContext() throws Exception {
2121
// Context of the app under test.
2222
Context appContext = InstrumentationRegistry.getTargetContext();
2323

24-
assertEquals("com.vogella.android.retrofitgithubcomment", appContext.getPackageName());
24+
assertEquals("com.vogella.android.retrofitgithub", appContext.getPackageName());
2525
}
2626
}

com.vogella.android.retrofitgithubcomment/app/src/main/AndroidManifest.xml renamed to com.vogella.android.retrofitgithub/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.vogella.android.retrofitgithubcomment">
3+
package="com.vogella.android.retrofitgithub">
44

55
<uses-permission android:name="android.permission.INTERNET"/>
66

@@ -10,7 +10,7 @@
1010
android:label="@string/app_name"
1111
android:supportsRtl="true"
1212
android:theme="@style/AppTheme">
13-
<activity android:name=".MainActivity">
13+
<activity android:name="com.vogella.android.retrofitgithub.MainActivity">
1414
<intent-filter>
1515
<action android:name="android.intent.action.MAIN" />
1616

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.vogella.android.retrofitgithub;
2+
3+
import android.app.Dialog;
4+
import android.content.Context;
5+
import android.content.DialogInterface;
6+
import android.os.Bundle;
7+
import android.support.annotation.NonNull;
8+
import android.support.v4.app.DialogFragment;
9+
import android.support.v7.app.AlertDialog;
10+
import android.view.View;
11+
import android.widget.EditText;
12+
13+
import okhttp3.Credentials;
14+
15+
public class CredentialsDialog extends DialogFragment {
16+
17+
EditText usernameEditText;
18+
EditText passwordEditText;
19+
ICredentialsDialogListener listener;
20+
21+
public interface ICredentialsDialogListener {
22+
void onDialogPositiveClick(String username, String password);
23+
}
24+
25+
@Override
26+
public void onAttach(Context context) {
27+
super.onAttach(context);
28+
if (getActivity() instanceof ICredentialsDialogListener) {
29+
listener = (ICredentialsDialogListener) getActivity();
30+
}
31+
}
32+
33+
@NonNull
34+
@Override
35+
public Dialog onCreateDialog(Bundle savedInstanceState) {
36+
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_credentials, null);
37+
usernameEditText = (EditText) view.findViewById(R.id.username_edittext);
38+
passwordEditText = (EditText) view.findViewById(R.id.password_edittext);
39+
40+
usernameEditText.setText(getArguments().getString("username"));
41+
passwordEditText.setText(getArguments().getString("password"));
42+
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
43+
.setView(view)
44+
.setTitle("Credentials")
45+
.setNegativeButton("Cancel", null)
46+
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
47+
@Override
48+
public void onClick(DialogInterface dialog, int which) {
49+
if (listener != null) {
50+
listener.onDialogPositiveClick(usernameEditText.getText().toString(), passwordEditText.getText().toString());
51+
}
52+
}
53+
});
54+
return builder.create();
55+
}
56+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.vogella.android.retrofitgithub;
2+
3+
import java.util.List;
4+
5+
import io.reactivex.Single;
6+
import okhttp3.ResponseBody;
7+
import retrofit2.http.Body;
8+
import retrofit2.http.GET;
9+
import retrofit2.http.POST;
10+
import retrofit2.http.Path;
11+
import retrofit2.http.Url;
12+
13+
public interface GithubAPI {
14+
String ENDPOINT = "https://api.github.com";
15+
16+
@GET("user/repos?per_page=100")
17+
Single<List<GithubRepo>> getRepos();
18+
19+
@GET("/repos/{owner}/{repo}/issues")
20+
Single<List<GithubIssue>> getIssues(@Path("owner") String owner, @Path("repo") String repository);
21+
22+
@POST
23+
Single<ResponseBody> postComment(@Url String url, @Body GithubIssue issue);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.vogella.android.retrofitgithub;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class GithubIssue {
6+
7+
String id;
8+
String title;
9+
String comments_url;
10+
11+
@SerializedName("body")
12+
String comment;
13+
14+
@Override
15+
public String toString() {
16+
return id + " - " + title;
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.vogella.android.retrofitgithub;
2+
3+
public class GithubRepo {
4+
String name;
5+
String owner;
6+
String url;
7+
8+
@Override
9+
public String toString() {
10+
return(name + " " + url);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.vogella.android.retrofitgithub;
2+
3+
import com.google.gson.JsonDeserializationContext;
4+
import com.google.gson.JsonDeserializer;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.JsonParseException;
8+
9+
import java.lang.reflect.Type;
10+
11+
public class GithubRepoDeserializer implements JsonDeserializer<GithubRepo> {
12+
13+
@Override
14+
public GithubRepo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
15+
GithubRepo githubRepo = new GithubRepo();
16+
17+
JsonObject repoJsonObject = json.getAsJsonObject();
18+
githubRepo.name = repoJsonObject.get("name").getAsString();
19+
githubRepo.url = repoJsonObject.get("url").getAsString();
20+
21+
JsonElement ownerJsonElement = repoJsonObject.get("owner");
22+
JsonObject ownerJsonObject = ownerJsonElement.getAsJsonObject();
23+
githubRepo.owner = ownerJsonObject.get("login").getAsString();
24+
25+
return githubRepo;
26+
}
27+
}

0 commit comments

Comments
 (0)