-2

Here is my code so far, please assist me, I need a to sign into the website using post method username and password. I looked up examples online and used the wiki how to do this. I set everything inside of the button click as you see:

package com.example.work.try1;

import android.widget.Button;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.net.Uri;
import android.content.Intent;
import android.view.View;
import java.io.OutputStream; 
import java.net.HttpURLConnection;
import java.net.URL;

public class Launch extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_launch);

    final View buttonS = findViewById(R.id.button);
    buttonS.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Uri uri = Uri.parse("https://www.navanithiastrolife.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

    Button b = (Button) findViewById(R.id.button2);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            EditText e1 = (EditText) findViewById(R.id.editText);
            String username = e1.getText().toString();
            EditText e2 = (EditText) findViewById(R.id.editText3);
            String password = e2.getText().toString();

            OutputStream out = null;
            try {

                URL url = new URL("http://iconinfo.tech/mob_app/");

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                urlConnection.connect();

                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("username",username);
                urlConnection.setRequestProperty("password",password);
                urlConnection.setDoOutput(true);

                urlConnection.disconnect();

            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    });
}
}

Please assist, ok thx.

2 Answers2

0

you also use the volley library to call the API

i hope its helpful to you ..!

Sanjay Chauhan
  • 893
  • 7
  • 13
0

Your request is being done in the main thread, this is not right because it can block the main thread. For this background operation you must use a background thread such as Volley library, Retrofit or RoboSpice. To build the POST method you have to construct the data your post will contain. For example, In Volley you use a key value pair approach.

HaroldSer
  • 2,025
  • 2
  • 12
  • 23