Good day, programmers, just recently started learning how to develop mobile apps for Android, and encountered such a problem; you need to implement registration and authorization in your application using Post requests. As I understand the registration (login) is performed using the post request for the corresponding endpoints, the request body should be passed json, for example:
{"username": "user1", "password": "1234"}
After registration (login) returns a token or error message.
As I have little experience, I don’t know how to implement the text fields for entering the login password to be converted to json format, and sent by post to the corresponding api / register (login).
Maybe someone has an example of the implementation of a similar task or an Internet source where you can learn some material for the implementation of this task.
Made a form to enter a login and password.
public class LoginActivity extends AppCompatActivity { private EditText emailText; private EditText passwordText; private Button loginBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login1); initComponent(); loginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if ((!emailText.getText().toString().equals("")) && (!passwordText.getText().toString().equals(""))) { } else if ((!emailText.getText().toString().equals(""))) { Toast.makeText(getApplicationContext(), "Password field empty", Toast.LENGTH_SHORT).show(); } else if ((!emailText.getText().toString().equals(""))) { Toast.makeText(getApplicationContext(), "Login name field empty", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Login name and Password field are empty", Toast.LENGTH_SHORT).show(); } } }); } private void initComponent() { emailText = (EditText) findViewById(R.id.input_email); passwordText = (EditText) findViewById(R.id.input_password); loginBtn = (Button) findViewById(R.id.btn_login); } }
I wrote a POST request using your example for registration, but it does not create an account at http://smktesting.herokuapp.com/api/register/ .
public class PostReg { private static final String TAG ="mee"; public static String doPost(String url, String jsonString) throws Exception { final String login = "yxz"; final String password ="test"; //jsonString = object.toString(); URL obj = new URL("http://smktesting.herokuapp.com/api/register/"); HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); //add reuqest header connection.setRequestProperty("User-Agent", "Mozilla/5.0"); connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); connection.setRequestProperty("Content-Type", "text/plain"); connection.setReadTimeout(15000); connection.setConnectTimeout(15000); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); JSONObject jsonParam = new JSONObject(); jsonParam.put("usernam", login); jsonParam.put("password", password); Uri.Builder builder = new Uri.Builder() .appendQueryParameter("username", login) .appendQueryParameter("password", password); String urlParameters = builder.build().getEncodedQuery(); OutputStream dStream = new BufferedOutputStream(connection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(dStream, "utf-8")); //Send request //DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); //outputStream.writeBytes(jsonString); writer.write(urlParameters); writer.flush(); writer.close(); int responseCode = connection.getResponseCode(); Log.d(TAG,"\n-----------Send http request-----------"); Log.d(TAG, "\nSending request to URL : " + url); Log.d(TAG, "Post parameters : " + jsonString); Log.d(TAG, "Response Code : " + responseCode); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = bufferedReader.readLine()) != null) { response.append(inputLine); } bufferedReader.close(); // print result Log.d(TAG,"Response string: " + response.toString()); Log.d(TAG,"-----------end http request-----------\n"); Log.d(TAG, " "); return response.toString(); } }