Guest User

Untitled

a guest
Apr 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.93 KB | None | 0 0
  1. public class LoginRequest extends Request<String> {
  2.  
  3. // ... other methods go here
  4.  
  5. private Map<String, String> mParams;
  6.  
  7. public LoginRequest(String param1, String param2, Listener<String> listener, ErrorListener errorListener) {
  8. super(Method.POST, "http://test.url", errorListener);
  9. mListener = listener;
  10. mParams = new HashMap<String, String>();
  11. mParams.put("paramOne", param1);
  12. mParams.put("paramTwo", param2);
  13.  
  14. }
  15.  
  16. @Override
  17. public Map<String, String> getParams() {
  18. return mParams;
  19. }
  20. }
  21.  
  22. String uri = String.format("http://somesite.com/some_endpoint.php?param1=%1$s&param2=%2$s",
  23. num1,
  24. num2);
  25.  
  26. StringRequest myReq = new StringRequest(Method.GET,
  27. uri,
  28. createMyReqSuccessListener(),
  29. createMyReqErrorListener());
  30. queue.add(myReq);
  31.  
  32. StringRequest myReq = new StringRequest(Method.POST,
  33. "http://somesite.com/some_endpoint.php",
  34. createMyReqSuccessListener(),
  35. createMyReqErrorListener()) {
  36.  
  37. protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
  38. Map<String, String> params = new HashMap<String, String>();
  39. params.put("param1", num1);
  40. params.put("param2", num2);
  41. return params;
  42. };
  43. };
  44. queue.add(myReq);
  45.  
  46. import java.io.UnsupportedEncodingException;
  47. import java.util.Map;
  48. import org.json.JSONException;
  49. import org.json.JSONObject;
  50. import com.android.volley.NetworkResponse;
  51. import com.android.volley.ParseError;
  52. import com.android.volley.Request;
  53. import com.android.volley.Response;
  54. import com.android.volley.Response.ErrorListener;
  55. import com.android.volley.Response.Listener;
  56. import com.android.volley.toolbox.HttpHeaderParser;
  57.  
  58. public class CustomRequest extends Request<JSONObject> {
  59.  
  60. private Listener<JSONObject> listener;
  61. private Map<String, String> params;
  62.  
  63. public CustomRequest(String url, Map<String, String> params,
  64. Listener<JSONObject> reponseListener, ErrorListener errorListener) {
  65. super(Method.GET, url, errorListener);
  66. this.listener = reponseListener;
  67. this.params = params;
  68. }
  69.  
  70. public CustomRequest(int method, String url, Map<String, String> params,
  71. Listener<JSONObject> reponseListener, ErrorListener errorListener) {
  72. super(method, url, errorListener);
  73. this.listener = reponseListener;
  74. this.params = params;
  75. }
  76.  
  77. protected Map<String, String> getParams()
  78. throws com.android.volley.AuthFailureError {
  79. return params;
  80. };
  81.  
  82. @Override
  83. protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
  84. try {
  85. String jsonString = new String(response.data,
  86. HttpHeaderParser.parseCharset(response.headers));
  87. return Response.success(new JSONObject(jsonString),
  88. HttpHeaderParser.parseCacheHeaders(response));
  89. } catch (UnsupportedEncodingException e) {
  90. return Response.error(new ParseError(e));
  91. } catch (JSONException je) {
  92. return Response.error(new ParseError(je));
  93. }
  94. }
  95.  
  96. @Override
  97. protected void deliverResponse(JSONObject response) {
  98. // TODO Auto-generated method stub
  99. listener.onResponse(response);
  100. }
  101.  
  102. }
  103.  
  104. import java.io.UnsupportedEncodingException;
  105. import java.util.Iterator;
  106. import java.util.Map;
  107.  
  108. import org.json.JSONException;
  109. import org.json.JSONObject;
  110.  
  111. import com.android.volley.NetworkResponse;
  112. import com.android.volley.ParseError;
  113. import com.android.volley.Request;
  114. import com.android.volley.Response;
  115. import com.android.volley.Response.ErrorListener;
  116. import com.android.volley.Response.Listener;
  117. import com.android.volley.toolbox.HttpHeaderParser;
  118.  
  119. public class CustomRequest extends Request<JSONObject> {
  120. private int mMethod;
  121. private String mUrl;
  122. private Map<String, String> mParams;
  123. private Listener<JSONObject> mListener;
  124.  
  125. public CustomRequest(int method, String url, Map<String, String> params,
  126. Listener<JSONObject> reponseListener, ErrorListener errorListener) {
  127. super(method, url, errorListener);
  128. this.mMethod = method;
  129. this.mUrl = url;
  130. this.mParams = params;
  131. this.mListener = reponseListener;
  132. }
  133.  
  134. @Override
  135. public String getUrl() {
  136. if(mMethod == Request.Method.GET) {
  137. if(mParams != null) {
  138. StringBuilder stringBuilder = new StringBuilder(mUrl);
  139. Iterator<Map.Entry<String, String>> iterator = mParams.entrySet().iterator();
  140. int i = 1;
  141. while (iterator.hasNext()) {
  142. Map.Entry<String, String> entry = iterator.next();
  143. if (i == 1) {
  144. stringBuilder.append("?" + entry.getKey() + "=" + entry.getValue());
  145. } else {
  146. stringBuilder.append("&" + entry.getKey() + "=" + entry.getValue());
  147. }
  148. iterator.remove(); // avoids a ConcurrentModificationException
  149. i++;
  150. }
  151. mUrl = stringBuilder.toString();
  152. }
  153. }
  154. return mUrl;
  155. }
  156.  
  157. @Override
  158. protected Map<String, String> getParams()
  159. throws com.android.volley.AuthFailureError {
  160. return mParams;
  161. };
  162.  
  163. @Override
  164. protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
  165. try {
  166. String jsonString = new String(response.data,
  167. HttpHeaderParser.parseCharset(response.headers));
  168. return Response.success(new JSONObject(jsonString),
  169. HttpHeaderParser.parseCacheHeaders(response));
  170. } catch (UnsupportedEncodingException e) {
  171. return Response.error(new ParseError(e));
  172. } catch (JSONException je) {
  173. return Response.error(new ParseError(je));
  174. }
  175. }
  176.  
  177. @Override
  178. protected void deliverResponse(JSONObject response) {
  179. // TODO Auto-generated method stub
  180. mListener.onResponse(response);
  181. }
  182. }
  183.  
  184. public void GET(String url, Map<String, String> params, Response.Listener<String> response_listener, Response.ErrorListener error_listener, String API_KEY, String stringRequestTag) {
  185. final Map<String, String> mParams = params;
  186. final String mAPI_KEY = API_KEY;
  187. final String mUrl = url;
  188.  
  189. StringRequest stringRequest = new StringRequest(
  190. Request.Method.GET,
  191. mUrl,
  192. response_listener,
  193. error_listener
  194. ) {
  195. @Override
  196. protected Map<String, String> getParams() {
  197. return mParams;
  198. }
  199.  
  200. @Override
  201. public String getUrl() {
  202. StringBuilder stringBuilder = new StringBuilder(mUrl);
  203. int i = 1;
  204. for (Map.Entry<String,String> entry: mParams.entrySet()) {
  205. String key;
  206. String value;
  207. try {
  208. key = URLEncoder.encode(entry.getKey(), "UTF-8");
  209. value = URLEncoder.encode(entry.getValue(), "UTF-8");
  210. if(i == 1) {
  211. stringBuilder.append("?" + key + "=" + value);
  212. } else {
  213. stringBuilder.append("&" + key + "=" + value);
  214. }
  215. } catch (UnsupportedEncodingException e) {
  216. e.printStackTrace();
  217. }
  218. i++;
  219.  
  220. }
  221. String url = stringBuilder.toString();
  222.  
  223. return url;
  224. }
  225.  
  226. @Override
  227. public Map<String, String> getHeaders() {
  228. Map<String, String> headers = new HashMap<>();
  229. if (!(mAPI_KEY.equals(""))) {
  230. headers.put("X-API-KEY", mAPI_KEY);
  231. }
  232. return headers;
  233. }
  234. };
  235.  
  236. if (stringRequestTag != null) {
  237. stringRequest.setTag(stringRequestTag);
  238. }
  239.  
  240. mRequestQueue.add(stringRequest);
  241. }
  242.  
  243. private void loggedInToMainPage(final String emailName, final String passwordName) {
  244.  
  245. String tag_string_req = "req_login";
  246. StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://localhost/index", new Response.Listener<String>() {
  247. @Override
  248. public void onResponse(String response) {
  249. Log.d(TAG, "Login Response: " + response.toString());
  250. try {
  251. JSONObject jsonObject = new JSONObject(response);
  252. Boolean error = jsonObject.getBoolean("error");
  253. if (!error) {
  254.  
  255. String uid = jsonObject.getString("uid");
  256. JSONObject user = jsonObject.getJSONObject("user");
  257. String email = user.getString("email");
  258. String password = user.getString("password");
  259.  
  260.  
  261. session.setLogin(true);
  262. Intent intent = new Intent(getApplicationContext(), MainActivity.class);
  263. startActivity(intent);
  264. finish();
  265. Toast.makeText(getApplicationContext(), "its ok", Toast.LENGTH_SHORT).show();
  266. }
  267. } catch (JSONException e) {
  268. e.printStackTrace();
  269. }
  270.  
  271. }
  272. }, new Response.ErrorListener() {
  273. @Override
  274. public void onErrorResponse(VolleyError volleyError) {
  275. System.out.println("volley Error .................");
  276. }
  277. }) {
  278. @Override
  279. protected Map<String, String> getParams() throws AuthFailureError {
  280. Map<String, String> params = new HashMap<String, String>();
  281. params.put("tag", "login");
  282. params.put("email", emailName);
  283. params.put("password", passwordName);
  284. return params;
  285. }
  286. };
  287.  
  288.  
  289. MyApplication.getInstance().addToRequestQueue(stringRequest,tag_string_req);
  290. }
  291.  
  292. dependencies {
  293.  
  294. compile 'io.gloxey.gnm:network-manager:1.0.1'
  295. }
  296.  
  297. ConnectionManager.volleyStringRequest(context, isDialog, progressDialogView, requestURL, volleyResponseInterface);
  298.  
  299. Configuration Description
  300.  
  301. Context Context
  302. isDialog If true dialog will appear, otherwise not.
  303. progressView For custom progress view supply your progress view id and make isDialog true. otherwise pass null.
  304. requestURL Pass your API URL.
  305. volleyResponseInterface Callback for response.
  306.  
  307. ConnectionManager.volleyStringRequest(this, false, null, "url", new VolleyResponse() {
  308. @Override
  309. public void onResponse(String _response) {
  310.  
  311. /**
  312. * Handle Response
  313. */
  314. }
  315.  
  316. @Override
  317. public void onErrorResponse(VolleyError error) {
  318.  
  319. /**
  320. * handle Volley Error
  321. */
  322. }
  323.  
  324. @Override
  325. public void isNetwork(boolean connected) {
  326.  
  327. /**
  328. * True if internet is connected otherwise false
  329. */
  330. }
  331. });
  332.  
  333. ConnectionManager.volleyStringRequest(context, isDialog, progressDialogView, requestURL, requestMethod, params, volleyResponseInterface);
  334.  
  335. Use Method : Request.Method.POST
  336. Request.Method.PUT
  337. Request.Method.DELETE
  338.  
  339. Your params :
  340.  
  341. HashMap<String, String> params = new HashMap<>();
  342. params.put("param 1", "value");
  343. params.put("param 2", "value");
  344.  
  345. ConnectionManager.volleyStringRequest(this, true, null, "url", Request.Method.POST, params, new VolleyResponse() {
  346. @Override
  347. public void onResponse(String _response) {
  348.  
  349. /**
  350. * Handle Response
  351. */
  352. }
  353.  
  354. @Override
  355. public void onErrorResponse(VolleyError error) {
  356.  
  357. /**
  358. * handle Volley Error
  359. */
  360. }
  361.  
  362. @Override
  363. public void isNetwork(boolean connected) {
  364.  
  365. /**
  366. * True if internet is connected otherwise false
  367. */
  368. }
  369. });
  370.  
  371. YourModel yourModel = GloxeyJsonParser.getInstance().parse(stringResponse, YourModel.class);
  372.  
  373. ConnectionManager.volleyStringRequest(this, false, null, "url", new VolleyResponse() {
  374. @Override
  375. public void onResponse(String _response) {
  376.  
  377. /**
  378. * Handle Response
  379. */
  380.  
  381. try {
  382.  
  383. YourModel yourModel = GloxeyJsonParser.getInstance().parse(_response, YourModel.class);
  384.  
  385. } catch (Exception e) {
  386. e.printStackTrace();
  387. }
  388.  
  389. }
  390.  
  391. @Override
  392. public void onErrorResponse(VolleyError error) {
  393.  
  394. /**
  395. * handle Volley Error
  396. */
  397. if (error instanceof TimeoutError || error instanceof NoConnectionError) {
  398.  
  399. showSnackBar(parentLayout, getString(R.string.internet_not_found), getString(R.string.retry), new View.OnClickListener() {
  400. @Override
  401. public void onClick(View view) {
  402.  
  403. //handle retry button
  404.  
  405. }
  406. });
  407.  
  408. } else if (error instanceof AuthFailureError) {
  409. } else if (error instanceof ServerError) {
  410. } else if (error instanceof NetworkError) {
  411. } else if (error instanceof ParseError) {
  412. }
  413.  
  414. }
  415.  
  416. @Override
  417. public void isNetwork(boolean connected) {
  418.  
  419. /**
  420. * True if internet is connected otherwise false
  421. */
  422. if (!connected) {
  423. showSnackBar(parentLayout, getString(R.string.internet_not_found), getString(R.string.retry), new View.OnClickListener() {
  424. @Override
  425. public void onClick(View view) {
  426. //Handle retry button
  427. }
  428. });
  429. }
  430. });
  431.  
  432.  
  433. public void showSnackBar(View view, String message) {
  434. Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
  435. }
  436.  
  437. public void showSnackBar(View view, String message, String actionText, View.OnClickListener onClickListener) {
  438. Snackbar.make(view, message, Snackbar.LENGTH_LONG).setAction(actionText, onClickListener).show();
  439. }
Add Comment
Please, Sign In to add comment