Skip to content

Commit

Permalink
internet connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Awais9 committed Dec 17, 2018
1 parent 2e1536e commit 999c6f7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 55 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
5 changes: 4 additions & 1 deletion volleysinglecall/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.awais.volleysinglecall" />
package="com.awais.volleysinglecall">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.android.volley.NetworkResponse;
Expand Down Expand Up @@ -92,53 +94,56 @@ public void setShowDialog(boolean showDialog) {
public <T> void callService(int requestType, String serviceName, final HashMap<String, String> hashMap,
final String tag, final Class<T> objectClass, final VolleyResponse calls,
JSONObject jsonObject, boolean isJsonReq) {
if (isNetworkAvailable(context)) {
if (isJsonReq) {
JsonObjectRequest objectRequest = new JsonObjectRequest(requestType, serviceName,
jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
requestSuccess("", response, tag, calls, objectClass);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
responseFailed(error, tag, calls);
}
}) {
@Override
public Map<String, String> getHeaders() {
return volleyQueue.getHeaders();
}
};
volleyQueue.addToRequestQueue(objectRequest, tag);
} else {
StringRequest request = new StringRequest(requestType,
serviceName, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
requestSuccess(response, null, tag, calls, objectClass);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
responseFailed(error, tag, calls);
}
}) {
@Override
public Map<String, String> getHeaders() {
return volleyQueue.getHeaders();
}

if (isJsonReq) {
JsonObjectRequest objectRequest = new JsonObjectRequest(requestType, serviceName,
jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
requestSuccess("", response, tag, calls, objectClass);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
responseFailed(error, tag, calls);
}
}) {
@Override
public Map<String, String> getHeaders() {
return volleyQueue.getHeaders();
}
};
volleyQueue.addToRequestQueue(objectRequest, tag);
@Override
protected Map<String, String> getParams() {
if (hashMap != null)
return hashMap;
else
return new HashMap<>();
}
};
volleyQueue.addToRequestQueue(request, tag);
}
} else {
StringRequest request = new StringRequest(requestType,
serviceName, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
requestSuccess(response, null, tag, calls, objectClass);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
responseFailed(error, tag, calls);
}
}) {
@Override
public Map<String, String> getHeaders() {
return volleyQueue.getHeaders();
}

@Override
protected Map<String, String> getParams() {
if (hashMap != null)
return hashMap;
else
return new HashMap<>();
}
};
volleyQueue.addToRequestQueue(request, tag);
showSingleDialog("Please check your internet connection", true);
}
}

Expand All @@ -165,11 +170,11 @@ private <T> void requestSuccess(String response, JSONObject responseObj, String
} else {
calls.onFailure(jsonObject.toString());
if (isShowDialog())
showSingleDialog(jsonObject.optString("message"));
showSingleDialog(jsonObject.optString("message"), false);
}
} catch (Exception e) {
if (isShowDialog())
showSingleDialog(e.getMessage());
showSingleDialog(e.getMessage(), false);
calls.onFailure(e.getMessage());
}
}
Expand All @@ -195,14 +200,14 @@ private void responseFailed(VolleyError error, String tag, VolleyResponse calls)
if (currentCount < getLogoutCount()) {
currentCount++;
if (isShowDialog())
showSingleDialog(error.toString());
showSingleDialog(error.toString(), false);
calls.onFailure(error.toString());
} else {
logoutDialog("Do you want to logout?");
}
} else {
if (isShowDialog())
showSingleDialog(error.toString());
showSingleDialog(error.toString(), false);
calls.onFailure(error.toString());
}
volleyQueue.cancelPendingRequests(tag);
Expand Down Expand Up @@ -272,7 +277,7 @@ public void setRequestHeaders(Header[] requestHeaders) {
});
}

private void showSingleDialog(String message) {
private void showSingleDialog(String message, final boolean fromNet) {
if (context != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
Expand All @@ -282,9 +287,11 @@ private void showSingleDialog(String message) {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
if (isFinishActivity()) {
Activity activity = (Activity) context;
activity.finish();
if (!fromNet) {
if (isFinishActivity()) {
Activity activity = (Activity) context;
activity.finish();
}
}
}
}).show();
Expand Down Expand Up @@ -316,4 +323,11 @@ public void onClick(DialogInterface dialogInterface, int i) {
Log.e("NetworkController", "Current context is null or expired.");
}
}

private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}

0 comments on commit 999c6f7

Please sign in to comment.