- I followed this tutorial: https://www.learn2crack.com/2014/04/android-login-registration-nodejs-server.html
- But that link is quite old (2014) and did not work right out of the box.
- I have updated the code so that it works in Feb 2017.
- The major problem is that you must now use HttpURLConnection as the other is now deprecated in his link.
Code
The following is the updated code for the RegisterActivity.java file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | package com.gerrycao.thething; import android.content.Intent; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class RegisterActivity extends AppCompatActivity { EditText email, password; Button login, register; String emailtxt, passwordtxt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); email = (EditText) findViewById(R.id.email); password = (EditText) findViewById(R.id.password); register = (Button) findViewById(R.id.registerbtn); login = (Button) findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent regactivity = new Intent(RegisterActivity.this, LoginActivity.class); startActivity(regactivity); finish(); } }); register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { emailtxt = email.getText().toString(); passwordtxt = password.getText().toString(); try { JSONObject json = new JSONObject(); json.put("email", emailtxt); json.put("password", passwordtxt); new AsyncHttpTask().execute(json); } catch (JSONException e) { e.printStackTrace(); } } }); } class AsyncHttpTask extends AsyncTask<JSONObject, Void, Integer> { @Override protected void onPreExecute() { } @Override protected Integer doInBackground(JSONObject... params) { Integer result = 0; HttpURLConnection urlConn = null; JSONObject json = params[0]; Log.d("postTask: JO", json.toString()); try { URL url; // NOTE: When I tried localhost, 127.0.0.1, or 10.10.x.x, those did not work for me. // I had to use my exact IP address to do debugging/testing. String address = "http://192.168.0.3:8080/register"; Log.d("sendPost", address); url = new URL(address); urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); urlConn.setRequestMethod("POST"); urlConn.setChunkedStreamingMode(100); urlConn.setRequestProperty("Content-Type", "application/json"); urlConn.connect(); byte[] outputBytes = json.toString().getBytes("UTF-8"); OutputStream os = urlConn.getOutputStream(); os.write(outputBytes); os.close(); result = urlConn.getResponseCode(); } catch (IOException e) { e.printStackTrace(); } finally { if (urlConn != null) urlConn.disconnect(); } return result; } } } |
More code snippets to come as I continue this app!
Contact
Please feel free to leave a comment or ask a question.