Advertisement
Guest User

Untitled

a guest
Apr 9th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. package pt.ulisboa.tecnico.cmov.project;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. import com.android.volley.AuthFailureError;
  13. import com.android.volley.NetworkResponse;
  14. import com.android.volley.RequestQueue;
  15. import com.android.volley.VolleyError;
  16. import com.android.volley.VolleyLog;
  17. import com.android.volley.toolbox.HttpHeaderParser;
  18. import com.android.volley.toolbox.StringRequest;
  19. import com.android.volley.toolbox.Volley;
  20.  
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23.  
  24. import java.io.IOException;
  25. import java.io.UnsupportedEncodingException;
  26.  
  27. import okhttp3.Call;
  28. import okhttp3.Callback;
  29. import okhttp3.MediaType;
  30. import okhttp3.OkHttpClient;
  31. import okhttp3.Request;
  32. import okhttp3.RequestBody;
  33. import okhttp3.Response;
  34.  
  35. public class SignUpActivity extends AppCompatActivity {
  36.  
  37. private Button sendPostReqButton;
  38.  
  39. public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  40.  
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.sign_up);
  45.  
  46. sendPostReqButton = (Button) findViewById(R.id.button4);
  47. sendPostReqButton.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View v) {
  50. testSignUp();
  51. }
  52. });
  53. }
  54.  
  55.  
  56. private void sendPostRequest(String username, String password){
  57.  
  58. final EditText usernameView = findViewById(R.id.editText2);
  59. final EditText passwordView = findViewById(R.id.editText3);
  60.  
  61. final Intent intent = new Intent(this, MainMenuActivity.class);
  62.  
  63. try{
  64. // Instantiate the RequestQueue.
  65. RequestQueue queue = Volley.newRequestQueue(this);
  66. JSONObject jsonBody = new JSONObject();
  67. jsonBody.put("username", usernameView.getText());
  68. jsonBody.put("password", passwordView.getText());
  69. final String mRequestBody = jsonBody.toString();
  70.  
  71. String url ="http://10.0.2.2:8080/user";
  72.  
  73. // Request a string response from the provided URL.
  74. StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
  75. new Response.Listener<String>() {
  76. private void onResponse(String response) {
  77. Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();
  78.  
  79. if(response.equals("User created with sucess!")){
  80. String username = usernameView.getText().toString();
  81. String password = passwordView.getText().toString();
  82.  
  83. startActivity(intent);
  84. }
  85. }
  86. }, new Response.ErrorListener() {
  87. @Override
  88. public void onErrorResponse(VolleyError error) {
  89. Toast.makeText(getApplicationContext(),"ERROR COMPLETING REQUEST", Toast.LENGTH_SHORT).show();
  90. }
  91.  
  92. }){
  93. }
  94. } catch (JSONException e) {
  95. e.printStackTrace();
  96. }
  97.  
  98.  
  99. void postRequest(String postUrl, String postBody) throws IOException {
  100.  
  101. OkHttpClient client = new OkHttpClient();
  102.  
  103. RequestBody body = RequestBody.create(JSON, postBody);
  104.  
  105. Request request = new Request.Builder()
  106. .url(postUrl)
  107. .post(body)
  108. .build();
  109.  
  110. client.newCall(request).enqueue(new Callback() {
  111. @Override
  112. public void onFailure(Call call, IOException e) {
  113. call.cancel();
  114. }
  115.  
  116. @Override
  117. public void onResponse(Call call, Response response) throws IOException {
  118. Intent intent = new Intent(SignUpActivity.this , MainMenuActivity.class);
  119.  
  120. Log.d("TAG",response.body().string());
  121. String r = response.body().string();
  122.  
  123. if (r == "User created with suscess") {
  124. startActivity(intent);
  125. } else {
  126. Log.d("TAG", "ERROR!!!!!!");
  127. }
  128. }
  129. });
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement