Skip to content

Commit

Permalink
new System with error Object
Browse files Browse the repository at this point in the history
  • Loading branch information
maxis7567 committed Jul 29, 2019
1 parent bfae892 commit 0a4e4d0
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 41 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "com.maxis7567.example"
minSdkVersion 14
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
Expand All @@ -19,7 +19,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.google.code.gson:gson:2.8.5'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.maxis7567.example">

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

<application
android:allowBackup="true"
Expand Down
23 changes: 16 additions & 7 deletions app/src/main/java/com/maxis7567/example/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.maxis7567.msvolley.JsonRequest;
import com.maxis7567.msvolley.LocalError;
import com.maxis7567.msvolley.PhpLaravelError;
import com.maxis7567.msvolley.RequestQueueContainer;
import com.maxis7567.msvolley.Respond;
import com.maxis7567.msvolley.RespondError;
import com.maxis7567.msvolley.ResponseError;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class Api {
public static void getBrandModel(Context context, Response.Listener<List<BrandModel>> DataListener, Response.ErrorListener errorListener){
JsonRequest<List<BrandModel>> getBrand=new JsonRequest<>(JsonRequest.Method.GET,
"https://my.api.mockaroo.com/carsbrand.json?key=",
new TypeToken<ArrayList<BrandModel>>(){}.getType(),
DataListener,
errorListener);
RequestQueueContainer.getRequestQueueContainer(context).add(getBrand);
public static void getBrandModel(Context context, Respond<BrandModel> DataListener, ResponseError<PhpLaravelError> errorRespondError, LocalError localError){
HashMap<String,String> header=new HashMap<>();
header.put("Accept", "application/json");
JsonRequest<BrandModel,PhpLaravelError> getBrand=new JsonRequest<>(JsonRequest.Method.POST,
"https://bejato.com/api/v1/login",header,
BrandModel.class,PhpLaravelError.class
, DataListener,errorRespondError,localError);
RequestQueueContainer.add(context,getBrand,localError);
}

}
22 changes: 22 additions & 0 deletions app/src/main/java/com/maxis7567/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,35 @@

import android.os.Bundle;

import com.android.volley.Response;
import com.maxis7567.example.R;
import com.maxis7567.msvolley.LocalError;
import com.maxis7567.msvolley.PhpLaravelError;
import com.maxis7567.msvolley.Respond;
import com.maxis7567.msvolley.RespondError;
import com.maxis7567.msvolley.ResponseError;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Api.getBrandModel(this, new Respond<BrandModel>() {
@Override
public void respond(BrandModel respond) {

}
}, new ResponseError<PhpLaravelError>() {
@Override
public void error(RespondError<PhpLaravelError> error) {
String a= error.getRespond().getErrors().get("mobile").getAsString();
}
}, new LocalError() {
@Override
public void error(String message) {

}
});
}
}
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -18,7 +18,6 @@ allprojects {
repositories {
google()
jcenter()

}
}

Expand Down
6 changes: 2 additions & 4 deletions msvolley/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 28
compileSdkVersion 29



defaultConfig {
minSdkVersion 14
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

Expand All @@ -26,10 +26,8 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.amitshekhar.android:android-networking:1.0.2'


}
5 changes: 4 additions & 1 deletion msvolley/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.maxis7567.msvolley" />
package="com.maxis7567.msvolley" >

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
78 changes: 58 additions & 20 deletions msvolley/src/main/java/com/maxis7567/msvolley/JsonRequest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.maxis7567.msvolley;


import android.content.Context;

import com.android.volley.NetworkResponse;

import com.android.volley.Request;
import com.android.volley.Response;

import com.android.volley.AuthFailureError;
import com.android.volley.ParseError;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
Expand All @@ -16,40 +17,56 @@
import java.lang.reflect.Type;
import java.util.Map;

public class JsonRequest<T> extends Request<T> {
public class JsonRequest<T, E> extends Request<T> {
private final Respond<T> responseListener;
private ResponseError<E> responseError;
private LocalError localError;
private Gson gson = new Gson();
private String body;
private final Map<String, String> header;
private Map<String, String> header;
private Type type;
private Response.Listener<T> responseLisener;
private Type errType;


public JsonRequest(int method, String url, Type type, Response.Listener<T> responseLisener, Response.ErrorListener listener) {
super(method, url, listener);
public JsonRequest(int method, String url, Type type, Type errType, Respond<T> responseListener, ResponseError<E> responseError,LocalError localError) {
super(method, url, null);
this.type = type;
this.responseLisener = responseLisener;
this.errType = errType;
this.responseListener = responseListener;
this.responseError = responseError;
this.localError = localError;
body = null;
header = null;
}

public JsonRequest(int method, String url, String body, Type type, Response.Listener<T> responseLisener, Response.ErrorListener listener) {
super(method, url, listener);
public JsonRequest(int method, String url, String body, Type type, Type errType, Respond<T> responseListener, ResponseError<E> responseError,LocalError localError) {
super(method, url, null);
this.body = body;
this.type = type;
this.responseLisener = responseLisener;
this.errType = errType;
this.responseListener = responseListener;
this.responseError = responseError;
this.localError = localError;
header = null;
}
public JsonRequest(int method, String url, Map<String,String> header, Type type, Response.Listener<T> responseLisener, Response.ErrorListener listener) {
super(method, url, listener);
public JsonRequest(int method, String url, Map<String, String> header, Type type, Type errType, Respond<T> responseListener, ResponseError<E> responseError,LocalError localError) {
super(method, url, null);
this.header = header;
this.type = type;
this.responseLisener = responseLisener;
this.errType = errType;
this.responseListener = responseListener;
this.responseError = responseError;
this.localError = localError;
}
public JsonRequest(int method, String url, String body, Map<String,String> header, Type type, Response.Listener<T> responseLisener, Response.ErrorListener listener) {
super(method, url, listener);
public JsonRequest(int method, String url, String body, Map<String, String> header, Type type, Type errType, Respond<T> responseListener, ResponseError<E> responseError,LocalError localError) {
super(method, url, null);
this.body = body;
this.header = header;
this.type = type;
this.responseLisener = responseLisener;
this.errType = errType;
this.responseListener = responseListener;
this.responseError = responseError;
this.localError = localError;
}

@Override
Expand All @@ -59,15 +76,35 @@ protected Response<T> parseNetworkResponse(NetworkResponse response) {
T respone = gson.fromJson(stringResponse, type);
return Response.success(respone, null);
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
localError.error(e.toString());
return null;
}catch (JsonParseException e){
return Response.error(new VolleyError(e.getMessage()));
localError.error(e.toString());
return null;
}
}

@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
if (volleyError.networkResponse!=null) {
String stringResponse = null;
try {
stringResponse = new String(volleyError.networkResponse.data, "UTF-8");
} catch (UnsupportedEncodingException e) {
localError.error(e.getMessage());
}
E respone = gson.fromJson(stringResponse, errType);
RespondError<E> error= new RespondError<>(volleyError.getMessage(), volleyError.networkResponse.statusCode,respone);
responseError.error(error);
}else {
localError.error(volleyError.toString());
}
return null;
}
@Override
protected void deliverResponse(T response) {
responseLisener.onResponse(response);
if (response!=null) {
responseListener.respond(response);
}
}

@Override
Expand All @@ -87,4 +124,5 @@ public byte[] getBody() throws AuthFailureError {

}


}
5 changes: 5 additions & 0 deletions msvolley/src/main/java/com/maxis7567/msvolley/LocalError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.maxis7567.msvolley;

public interface LocalError {
void error(String message);
}
52 changes: 52 additions & 0 deletions msvolley/src/main/java/com/maxis7567/msvolley/NetworkCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.maxis7567.msvolley;


import android.annotation.TargetApi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;


public class NetworkCheck {

public static boolean isNetworkAvailable(Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
return hasInternetConnection(context);
}else {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
}
public static boolean isOnline() {
try {
Process p1 = Runtime.getRuntime().exec("ping -c 1 www.google.com");
int returnVal = p1.waitFor();
return (returnVal==0);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@TargetApi(Build.VERSION_CODES.M)
public static boolean hasInternetConnection(final Context context) {
final ConnectivityManager connectivityManager = (ConnectivityManager)context.
getSystemService(Context.CONNECTIVITY_SERVICE);

final Network network;

network = connectivityManager.getActiveNetwork();
final NetworkCapabilities capabilities = connectivityManager
.getNetworkCapabilities(network);

return capabilities != null
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);


}
}

27 changes: 27 additions & 0 deletions msvolley/src/main/java/com/maxis7567/msvolley/PhpLaravelError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.maxis7567.msvolley;

import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;

public class PhpLaravelError {
@SerializedName("message")
private String message;
@SerializedName("errors")
private JsonObject errors;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public JsonObject getErrors() {
return errors;
}

public void setErrors(JsonObject errors) {
this.errors = errors;
}
}
Loading

0 comments on commit 0a4e4d0

Please sign in to comment.