Created
May 17, 2018 02:26
-
-
Save DykiSA/7130944e993306e505500cc0214ab018 to your computer and use it in GitHub Desktop.
Ready to use HTTP request class for java android using valley library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// change this package name based on your current package | |
package com.servertechno.e_bikes.activities; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import com.android.volley.VolleyError; | |
import com.servertechno.e_bikes.R; | |
import com.servertechno.e_bikes.constants.AppDictionary; | |
import com.servertechno.e_bikes.modules.Alert; | |
import com.servertechno.e_bikes.modules.HTTPRequest; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created by Acing on 5/1/18 19:19. | |
*/ | |
public class ExampleActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
// ... | |
// how to use httpRequest | |
final Context mContext = this; | |
Map<String, String> params = new HashMap<>(); | |
// params.put(AppDictionary.Driver.PHONE, phoneNumber); | |
params.put("phone_number", phoneNumber); | |
HTTPRequest http = new HTTPRequest(); | |
http.setParam(params); | |
http.setUrl(AppDictionary.Api.LOGIN_URL); | |
http.setMethod(http.POST_REQUEST); | |
http.execute(mContext, new HTTPRequest.Listener() { | |
public void onRequestSuccess(String response) throws JSONException { | |
Log.d("http response", response); | |
// conver to java json object | |
JSONObject obj = new JSONObject(response.trim()); | |
// get value from spesifiec key | |
try { | |
Log.d("status", obj.getString("status")); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onRequestError(VolleyError error) { | |
error.printStackTrace(); | |
Alert.createAlert(mContext, "Request Error", error.getMessage()); | |
} | |
@Override | |
public void onNetworkUnavailable() { | |
Alert.createAlert(mContext, "Request Error", "Network is unavailable"); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// change this package name based on your current package | |
package com.servertechno.e_bikes.modules; | |
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.util.Log; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
import org.json.JSONException; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created by Acing on 12/2/2015. | |
*/ | |
public class HTTPRequest { | |
private Map<String, String> params; | |
private int reqMethod; | |
private String url; | |
public int GET_REQUEST = Request.Method.GET; | |
public int POST_REQUEST = Request.Method.POST; | |
private RequestQueue requestQueue; | |
private String username = "user_api"; | |
private String password = "Axa0DZzrTgnIliOynzCtVgF1"; | |
public HTTPRequest setParam(Map<String, String> params){ | |
this.params = params; | |
return this; | |
} | |
public HTTPRequest setUrl(String url){ | |
this.url = url; | |
return this; | |
} | |
public HTTPRequest setMethod(int reqMethod){ | |
this.reqMethod = reqMethod; | |
return this; | |
} | |
public void execute(Context mContext, final Listener callback){ | |
if (params == null){ | |
this.params = new HashMap<>(); | |
} else { | |
Log.d("parameter", this.params.toString()); | |
} | |
if (isNetworkAvailable(mContext)){ | |
try { | |
final StringRequest request = new StringRequest(reqMethod, | |
this.url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
try { | |
callback.onRequestSuccess(response); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
callback.onRequestError(error); | |
} | |
}) | |
{ | |
@Override | |
protected Map<String, String> getParams() throws AuthFailureError { | |
// override params | |
// HTTPRequest.this.params.put(AppDictionary.Api.SECRET_LABEL, AppDictionary.Api.SECRET_KEY); | |
// return the params | |
return HTTPRequest.this.params; | |
} | |
// @Override | |
// public Map<String, String> getHeaders() throws AuthFailureError { | |
// // manipulates header informations | |
// Map<String, String> params = new HashMap<>(); | |
// String str = Base64.encodeToString(String.format("%s:%s", username, password).getBytes(), Base64.DEFAULT); | |
// String auth = String.format("Basic %s", str); | |
// params.put("Authorization", auth); | |
// return params; | |
// } | |
}; | |
if (requestQueue == null){ | |
requestQueue = Volley.newRequestQueue(mContext); | |
} | |
requestQueue.add(request); | |
} catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} else { | |
// network is unavailable | |
// call listener | |
callback.onNetworkUnavailable(); | |
} | |
} | |
private boolean isNetworkAvailable(Context mContext){ | |
ConnectivityManager cm = (ConnectivityManager) mContext | |
.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); | |
return activeNetwork != null && activeNetwork.isConnected(); | |
}; | |
public interface Listener { | |
void onRequestSuccess(String response) throws JSONException; | |
void onRequestError(VolleyError error); | |
void onNetworkUnavailable(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment