Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. protected void getLogin() {
  2. final String mURL = "https://somesite.com/api/login";
  3.  
  4. EditText username = (EditText) findViewById(R.id.username);
  5. EditText password = (EditText) findViewById(R.id.password);
  6.  
  7. // Post params to be sent to the server
  8. HashMap<String, String> params = new HashMap<String, String>();
  9. params.put("username", username.getText().toString());
  10. params.put("password", password.getText().toString());
  11.  
  12. JsonObjectRequest req = new JsonObjectRequest(mURL, new JSONObject(
  13. params), new Response.Listener<JSONObject>() {
  14. @Override
  15. public void onResponse(JSONObject response) {
  16.  
  17. try {
  18. JSONObject obj = response
  19. .getJSONObject("some_json_obj");
  20.  
  21. Log.w("myApp",
  22. "status code..." + obj.getString("name"));
  23.  
  24. // VolleyLog.v("Response:%n %s", response.toString(4));
  25.  
  26. } catch (JSONException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }, new Response.ErrorListener() {
  31. @Override
  32. public void onErrorResponse(VolleyError error) {
  33. Log.w("error in response", "Error: " + error.getMessage());
  34. }
  35. });
  36.  
  37. // add the request object to the queue to be executed
  38. AppController.getInstance().addToRequestQueue(req);
  39. }
  40.  
  41. /* import com.android.volley.toolbox.HttpHeaderParser; */
  42. public void onErrorResponse(VolleyError error) {
  43.  
  44. // As of f605da3 the following should work
  45. NetworkResponse response = error.networkResponse;
  46. if (error instanceof ServerError && response != null) {
  47. try {
  48. String res = new String(response.data,
  49. HttpHeaderParser.parseCharset(response.headers, "utf-8"));
  50. // Now you can use any deserializer to make sense of data
  51. JSONObject obj = new JSONObject(res);
  52. } catch (UnsupportedEncodingException e1) {
  53. // Couldn't properly decode data to string
  54. e1.printStackTrace();
  55. } catch (JSONException e2) {
  56. // returned data is not JSONObject?
  57. e2.printStackTrace();
  58. }
  59. }
  60. }
  61.  
  62. @Override
  63. public Map<String, String> getHeaders() throws AuthFailureError {
  64. HashMap<String, String> headers = new HashMap<String, String>();
  65. headers.put("Content-Type", "application/json; charset=utf-8");
  66. return headers;
  67. }
  68.  
  69. StringRequest sr = new StringRequest(type,url, new Response.Listener<String>() {
  70. @Override
  71. public void onResponse(String response) {
  72.  
  73. // valid response
  74. }
  75. }, new Response.ErrorListener() {
  76. @Override
  77. public void onErrorResponse(VolleyError error) {
  78. // error
  79. }
  80. }){
  81.  
  82. @Override
  83. protected Map<String,String> getParams(){
  84. Map<String,String> params = new HashMap<String, String>();
  85. params.put("username", username);
  86. params.put("password", password);
  87. params.put("grant_type", "password");
  88. return params;
  89. }
  90.  
  91. @Override
  92. public Map<String, String> getHeaders() throws AuthFailureError {
  93. Map<String,String> params = new HashMap<String, String>();
  94. // Removed this line if you dont need it or Use application/json
  95. // params.put("Content-Type", "application/x-www-form-urlencoded");
  96. return params;
  97. }
  98.  
  99. protected void getLogin() {
  100.  
  101. EditText username = (EditText) findViewById(R.id.username);
  102. EditText password = (EditText) findViewById(R.id.password);
  103.  
  104. RequestParams params = new RequestParams();
  105. params.put("username", username.getText().toString());
  106. params.put("password", password.getText().toString());
  107.  
  108. RestClient.post(getHost() + "api/v1/auth/login", params,
  109. new JsonHttpResponseHandler() {
  110.  
  111. @Override
  112. public void onSuccess(int statusCode, Header[] headers,
  113. JSONObject response) {
  114.  
  115. try {
  116.  
  117. //process JSONObject obj
  118. Log.w("myapp","success status code..." + statusCode);
  119.  
  120. } catch (JSONException e) {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. }
  124. }
  125.  
  126. @Override
  127. public void onFailure(int statusCode, Header[] headers,
  128. Throwable throwable, JSONObject errorResponse) {
  129. Log.w("myapp", "failure status code..." + statusCode);
  130.  
  131.  
  132. try {
  133. //process JSONObject obj
  134. Log.w("myapp", "error ..." + errorResponse.getString("message").toString());
  135. } catch (JSONException e) {
  136. // TODO Auto-generated catch block
  137. e.printStackTrace();
  138. }
  139. }
  140. });
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement