Advertisement
Guest User

hw3c121

a guest
Apr 28th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. package com.dealfaro.luca.androidhomephone;
  2.  
  3. import android.net.Uri;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.TextView;
  9.  
  10. import com.android.volley.AuthFailureError;
  11. import com.android.volley.Request;
  12. import com.android.volley.RequestQueue;
  13. import com.android.volley.Response;
  14. import com.android.volley.VolleyError;
  15. import com.android.volley.toolbox.JsonObjectRequest;
  16. import com.android.volley.toolbox.StringRequest;
  17. import com.android.volley.toolbox.Volley;
  18.  
  19. import org.json.JSONArray;
  20. import org.json.JSONObject;
  21.  
  22. import java.net.URLEncoder;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25.  
  26. import static android.webkit.ConsoleMessage.MessageLevel.LOG;
  27.  
  28.  
  29. public class MainActivity extends AppCompatActivity {
  30.  
  31. private final String LOG_TAG = "androidhomephone";
  32.  
  33. RequestQueue queue;
  34.  
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39. queue = Volley.newRequestQueue(this);
  40.  
  41. }
  42.  
  43. @Override
  44. protected void onResume() {
  45. super.onResume();
  46.  
  47. // We need the "final" keyword here to guarantee that the
  48. // value does not change, as it is used in the callbacks.
  49.  
  50. }
  51.  
  52. private void sendMsg(final String recipient) {
  53.  
  54. StringRequest sr = new StringRequest(Request.Method.POST,
  55. "https://luca-ucsc-teaching-backend.appspot.com/hw3/request_via_post",
  56. new Response.Listener<String>() {
  57. @Override
  58. public void onResponse(String response) {
  59. Log.d(LOG_TAG, "Post:" + response);
  60. TextView tv = (TextView) findViewById(R.id.my_text);
  61. tv.setText("Post: " + response.toString());
  62. }
  63. }, new Response.ErrorListener() {
  64. @Override
  65. public void onErrorResponse(VolleyError error) {
  66. }
  67. }) {
  68. @Override
  69. protected Map<String, String> getParams() {
  70. Map<String, String> params = new HashMap<String, String>();
  71. params.put("token", recipient);
  72. return params;
  73. }
  74.  
  75. @Override
  76. public Map<String, String> getHeaders() throws AuthFailureError {
  77. Map<String, String> params = new HashMap<String, String>();
  78. params.put("Content-Type", "application/x-www-form-urlencoded");
  79. return params;
  80. }
  81. };
  82. queue.add(sr);
  83.  
  84. }
  85.  
  86.  
  87. private void getMessages(final String recipient) {
  88.  
  89. // Instantiate the RequestQueue.
  90. String url = "https://luca-ucsc-teaching-backend.appspot.com/hw3/request_via_get";
  91.  
  92. String my_url = url + "?token=" + URLEncoder.encode(recipient);
  93.  
  94. JsonObjectRequest jsObjRequest = new JsonObjectRequest
  95. (Request.Method.GET, my_url, null, new Response.Listener<JSONObject>() {
  96.  
  97. @Override
  98. public void onResponse(JSONObject response) {
  99. Log.d(LOG_TAG, "Received: " + response.toString());
  100. TextView tv = (TextView) findViewById(R.id.my_text);
  101. tv.setText("Got: " + response.toString());
  102. // Ok, let's disassemble a bit the json object.
  103. }
  104. }, new Response.ErrorListener() {
  105.  
  106. @Override
  107. public void onErrorResponse(VolleyError error) {
  108. // TODO Auto-generated method stub
  109. Log.d(LOG_TAG, error.toString());
  110. }
  111. });
  112.  
  113. // In some cases, we don't want to cache the request.
  114. // jsObjRequest.setShouldCache(false);
  115.  
  116. queue.add(jsObjRequest);
  117. }
  118.  
  119. public void get(View v) {
  120. getMessages("abracadabra");
  121. }
  122.  
  123. public void post(View v) {
  124. sendMsg("abracadabra");}
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement