Skip to content

Commit 96bd532

Browse files
author
Suneet Srivastava
committed
feat: Removed volley and shifted api calls to retrofit
1 parent 7c4f5cf commit 96bd532

File tree

7 files changed

+141
-104
lines changed

7 files changed

+141
-104
lines changed

app/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ android {
9292

9393
configurations {
9494
implementation.exclude group: 'org.jetbrains', module: 'annotations'
95-
all*.exclude group: 'com.android.volley'
9695
}
9796

9897
lintOptions {
@@ -145,7 +144,10 @@ dependencies {
145144
androidTestImplementation 'androidx.test:runner:1.2.0'
146145
androidTestImplementation 'androidx.test:rules:1.2.0'
147146

148-
implementation 'com.mcxiaoke.volley:library:1.0.19'
147+
//retrofit
148+
implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
149+
implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
150+
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.0'
149151

150152
//google and support
151153
implementation "androidx.appcompat:appcompat:$rootProject.supportLibraryVersion"

app/src/main/java/org/fossasia/phimpme/share/SharingActivity.java

Lines changed: 87 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import android.text.Editable;
3838
import android.text.TextUtils;
3939
import android.text.TextWatcher;
40+
import android.util.ArrayMap;
4041
import android.util.Log;
4142
import android.view.View;
4243
import android.view.WindowManager;
@@ -54,14 +55,6 @@
5455
import androidx.recyclerview.widget.RecyclerView;
5556
import butterknife.BindView;
5657
import butterknife.ButterKnife;
57-
import com.android.volley.AuthFailureError;
58-
import com.android.volley.DefaultRetryPolicy;
59-
import com.android.volley.Request;
60-
import com.android.volley.RequestQueue;
61-
import com.android.volley.Response;
62-
import com.android.volley.VolleyError;
63-
import com.android.volley.toolbox.StringRequest;
64-
import com.android.volley.toolbox.Volley;
6558
import com.box.androidsdk.content.BoxApiFile;
6659
import com.box.androidsdk.content.BoxConfig;
6760
import com.box.androidsdk.content.BoxException;
@@ -78,6 +71,7 @@
7871
import com.dropbox.core.v2.DbxClientV2;
7972
import com.dropbox.core.v2.files.WriteMode;
8073
import com.google.android.material.snackbar.Snackbar;
74+
import com.google.gson.JsonElement;
8175
import com.mikepenz.community_material_typeface_library.CommunityMaterial;
8276
import com.mikepenz.iconics.view.IconicsImageView;
8377
import com.owncloud.android.lib.common.OwnCloudClient;
@@ -109,8 +103,6 @@
109103
import java.text.SimpleDateFormat;
110104
import java.util.ArrayList;
111105
import java.util.Date;
112-
import java.util.HashMap;
113-
import java.util.Map;
114106
import org.fossasia.phimpme.R;
115107
import org.fossasia.phimpme.base.PhimpmeProgressBarHandler;
116108
import org.fossasia.phimpme.base.RecyclerItemClickListner;
@@ -125,12 +117,18 @@
125117
import org.fossasia.phimpme.share.tumblr.TumblrClient;
126118
import org.fossasia.phimpme.share.twitter.HelperMethods;
127119
import org.fossasia.phimpme.utilities.ActivitySwitchHelper;
120+
import org.fossasia.phimpme.utilities.ApiInterface;
128121
import org.fossasia.phimpme.utilities.Constants;
129122
import org.fossasia.phimpme.utilities.NotificationHandler;
123+
import org.fossasia.phimpme.utilities.RetrofitClient;
130124
import org.fossasia.phimpme.utilities.SnackBarHandler;
125+
import org.fossasia.phimpme.utilities.Urls;
131126
import org.fossasia.phimpme.utilities.Utils;
132127
import org.json.JSONException;
133128
import org.json.JSONObject;
129+
import retrofit2.Call;
130+
import retrofit2.Callback;
131+
import retrofit2.Response;
134132

135133
/**
136134
* Class which deals with Sharing images to multiple Account logged in by the user in the app. If
@@ -1081,107 +1079,95 @@ void uploadImgur() {
10811079
Bitmap bitmap = getBitmapFromPath(saveFilePath);
10821080
final String imageString = getStringImage(bitmap);
10831081
// sending image to server
1084-
StringRequest request =
1085-
new StringRequest(
1086-
Request.Method.POST,
1087-
Constants.IMGUR_IMAGE_UPLOAD_URL,
1088-
new Response.Listener<String>() {
1082+
ApiInterface retrofitClient =
1083+
RetrofitClient.getRetrofitClient(Urls.IMGUR_BASE_URL).create(ApiInterface.class);
1084+
ArrayMap<String, String> body = new ArrayMap<>();
1085+
body.put("image", imageString);
1086+
if (caption != null && !caption.isEmpty()) {
1087+
body.put("title", caption);
1088+
}
1089+
String authorization;
1090+
if (isPersonal && imgurAuth != null) {
1091+
authorization = imgurAuth;
1092+
} else {
1093+
authorization = getClientAuth();
1094+
}
1095+
retrofitClient
1096+
.uploadImageToImgur(authorization, body)
1097+
.enqueue(
1098+
new Callback<JsonElement>() {
10891099
@Override
1090-
public void onResponse(String s) {
1091-
dialog.dismiss();
1092-
JSONObject jsonObject = null;
1093-
1094-
try {
1095-
jsonObject = new JSONObject(s);
1096-
Boolean success = jsonObject.getBoolean("success");
1097-
if (success) {
1098-
final String url = jsonObject.getJSONObject("data").getString("link");
1100+
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
1101+
if (response.body() != null && response.isSuccessful()) {
1102+
dialog.dismiss();
1103+
JSONObject jsonObject = null;
1104+
try {
1105+
jsonObject = new JSONObject(response.body().toString());
1106+
boolean success = jsonObject.getBoolean("success");
1107+
if (success) {
1108+
final String url = jsonObject.getJSONObject("data").getString("link");
1109+
1110+
if (isPersonal) {
1111+
imgurString = getString(R.string.upload_personal) + "\n" + url;
1112+
} else {
1113+
imgurString = getString(R.string.upload_anonymous) + "\n" + url;
1114+
}
10991115

1100-
if (isPersonal) {
1101-
imgurString = getString(R.string.upload_personal) + "\n" + url;
1116+
AlertDialogsHelper.getTextDialog(
1117+
SharingActivity.this,
1118+
dialogBuilder,
1119+
R.string.imgur_uplaoded_dialog_title,
1120+
0,
1121+
imgurString);
1122+
dialogBuilder.setPositiveButton(
1123+
getString(R.string.share).toUpperCase(),
1124+
new DialogInterface.OnClickListener() {
1125+
@Override
1126+
public void onClick(DialogInterface dialogInterface, int i) {
1127+
shareMsgOnIntent(SharingActivity.this, url);
1128+
sendResult(Constants.SUCCESS);
1129+
}
1130+
});
1131+
1132+
dialogBuilder.setNeutralButton(
1133+
getString(R.string.copy_action).toUpperCase(),
1134+
new DialogInterface.OnClickListener() {
1135+
@Override
1136+
public void onClick(DialogInterface dialogInterface, int i) {
1137+
copyToClipBoard(SharingActivity.this, url);
1138+
sendResult(Constants.SUCCESS);
1139+
}
1140+
});
1141+
dialogBuilder.setNegativeButton(getString(R.string.exit).toUpperCase(), null);
1142+
AlertDialog alertDialog = dialogBuilder.create();
1143+
alertDialog.show();
1144+
AlertDialogsHelper.setButtonTextColor(
1145+
new int[] {
1146+
DialogInterface.BUTTON_POSITIVE,
1147+
DialogInterface.BUTTON_NEGATIVE,
1148+
DialogInterface.BUTTON_NEUTRAL
1149+
},
1150+
getAccentColor(),
1151+
alertDialog);
11021152
} else {
1103-
imgurString = getString(R.string.upload_anonymous) + "\n" + url;
1153+
SnackBarHandler.create(parent, getString(R.string.error_on_imgur)).show();
1154+
sendResult(FAIL);
11041155
}
1105-
1106-
AlertDialogsHelper.getTextDialog(
1107-
SharingActivity.this,
1108-
dialogBuilder,
1109-
R.string.imgur_uplaoded_dialog_title,
1110-
0,
1111-
imgurString);
1112-
dialogBuilder.setPositiveButton(
1113-
getString(R.string.share).toUpperCase(),
1114-
new DialogInterface.OnClickListener() {
1115-
@Override
1116-
public void onClick(DialogInterface dialogInterface, int i) {
1117-
shareMsgOnIntent(SharingActivity.this, url);
1118-
sendResult(Constants.SUCCESS);
1119-
}
1120-
});
1121-
1122-
dialogBuilder.setNeutralButton(
1123-
getString(R.string.copy_action).toUpperCase(),
1124-
new DialogInterface.OnClickListener() {
1125-
@Override
1126-
public void onClick(DialogInterface dialogInterface, int i) {
1127-
copyToClipBoard(SharingActivity.this, url);
1128-
sendResult(Constants.SUCCESS);
1129-
}
1130-
});
1131-
dialogBuilder.setNegativeButton(getString(R.string.exit).toUpperCase(), null);
1132-
AlertDialog alertDialog = dialogBuilder.create();
1133-
alertDialog.show();
1134-
AlertDialogsHelper.setButtonTextColor(
1135-
new int[] {
1136-
DialogInterface.BUTTON_POSITIVE,
1137-
DialogInterface.BUTTON_NEGATIVE,
1138-
DialogInterface.BUTTON_NEUTRAL
1139-
},
1140-
getAccentColor(),
1141-
alertDialog);
1142-
} else {
1143-
SnackBarHandler.create(parent, getString(R.string.error_on_imgur)).show();
1144-
sendResult(FAIL);
1156+
} catch (JSONException e) {
1157+
e.printStackTrace();
11451158
}
1146-
} catch (JSONException e) {
1147-
e.printStackTrace();
1159+
} else {
1160+
dialog.dismiss();
1161+
SnackBarHandler.create(parent, getString(R.string.error_volly)).show();
11481162
}
11491163
}
1150-
},
1151-
new Response.ErrorListener() {
1164+
11521165
@Override
1153-
public void onErrorResponse(VolleyError volleyError) {
1166+
public void onFailure(Call<JsonElement> call, Throwable t) {
11541167
dialog.dismiss();
1155-
SnackBarHandler.create(parent, getString(R.string.error_volly))
1156-
.show(); // add volleyError to check error
1168+
SnackBarHandler.create(parent, getString(R.string.error_volly)).show();
11571169
}
1158-
}) {
1159-
@Override
1160-
protected Map<String, String> getParams() throws AuthFailureError {
1161-
Map<String, String> parameters = new HashMap<String, String>();
1162-
parameters.put("image", imageString);
1163-
if (caption != null && !caption.isEmpty()) parameters.put("title", caption);
1164-
return parameters;
1165-
}
1166-
1167-
@Override
1168-
public Map<String, String> getHeaders() throws AuthFailureError {
1169-
Map<String, String> headers = new HashMap<String, String>();
1170-
if (isPersonal) {
1171-
if (imgurAuth != null) {
1172-
headers.put(getString(R.string.header_auth), imgurAuth);
1173-
}
1174-
} else {
1175-
headers.put(getString(R.string.header_auth), getClientAuth());
1176-
}
1177-
1178-
return headers;
1179-
}
1180-
};
1181-
request.setRetryPolicy(
1182-
new DefaultRetryPolicy(50000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
1183-
RequestQueue rQueue = Volley.newRequestQueue(SharingActivity.this);
1184-
rQueue.add(request);
1170+
});
11851171
}
11861172

11871173
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.fossasia.phimpme.utilities;
2+
3+
import android.util.ArrayMap;
4+
import com.google.gson.JsonElement;
5+
import retrofit2.Call;
6+
import retrofit2.http.Body;
7+
import retrofit2.http.Header;
8+
import retrofit2.http.POST;
9+
10+
/** Created by @codedsun on 19/Oct/2019 */
11+
public interface ApiInterface {
12+
13+
@POST("/3/image")
14+
Call<JsonElement> uploadImageToImgur(
15+
@Header("Authorization") String authorization, @Body ArrayMap<String, String> body);
16+
}

app/src/main/java/org/fossasia/phimpme/utilities/Constants.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class Constants {
88
public static final int REQUEST_SHARE_RESULT = 50;
99
public static final String SHARE_RESULT = "share_result";
1010

11-
public static final String IMGUR_IMAGE_UPLOAD_URL = "https://api.imgur.com/3/image/";
1211
public static String IMGUR_HEADER_CLIENt = "Client-ID";
1312
public static String IMGUR_HEADER_USER = "Bearer";
1413

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.fossasia.phimpme.utilities;
2+
3+
import okhttp3.OkHttpClient;
4+
import okhttp3.logging.HttpLoggingInterceptor;
5+
import retrofit2.Retrofit;
6+
import retrofit2.converter.gson.GsonConverterFactory;
7+
8+
/** Created by @codedsun on 19/Oct/2019 */
9+
public class RetrofitClient {
10+
11+
private static OkHttpClient.Builder httpClient() {
12+
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
13+
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
14+
interceptor.level(HttpLoggingInterceptor.Level.BODY);
15+
httpClient.interceptors().add(interceptor);
16+
return httpClient;
17+
}
18+
19+
public static Retrofit getRetrofitClient(String baseUrl) {
20+
return new Retrofit.Builder()
21+
.baseUrl(baseUrl)
22+
.client(httpClient().build())
23+
.addConverterFactory(GsonConverterFactory.create())
24+
.build();
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.fossasia.phimpme.utilities;
2+
3+
/** Created by @codedsun on 19/Oct/2019 */
4+
public class Urls {
5+
6+
public static final String IMGUR_BASE_URL = "https://api.imgur.com";
7+
}

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ ext {
5454
nextCloudVersion = "1.5.0"
5555
lifecycleVersion = "2.1.0"
5656
glideVersion = "4.10.0"
57+
retrofitVersion = "2.6.2"
5758
}
5859

0 commit comments

Comments
 (0)