0

I can't seem to find recent tutorials online because most of them (HttpClient for example) has been depreciated.

What I want to happen here is for users to login using their ID number and password from my Android App. The data can be found from my MySql Database in web.

Here is my connection: loginstudent.php

<?php

include('./db/dbconn.php');

$stud_idno = $_POST["idno"];
$stud_passw = $_POST["passw"];

$row=getStudent($stud_idno, $stud_passw);
header('Content-Type: application/json');
echo json_encode(array('student'=>$row));

?>

code to get the student information from my database: dbconn.php

function getStudent($idno, $passw){
    global $conn;
    $row;
    try{
        connect();
        $sql="SELECT * FROM student_tbl WHERE stud_idno=? and stud_passw=?";
        $stmt=$conn->prepare($sql);
  $stmt->execute(array($idno, $passw));
        $row=$stmt->fetch(PDO::FETCH_ASSOC);
    }catch(PDOException $e){ echo $e->getMessage();}
    return $row;
}

What my android app do is to display an alertdialog and ask for the localhost (already fixed) and then display another dialog that will ask for their login.

Here is a snippet of my MainActivity.php

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView listView;
    ArrayList<QuestionItem> arrayList = new ArrayList<QuestionItem>();
    ArrayAdapter<QuestionItem> adapter;
    String ip;

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

        //allow a parallel thread to run alongside
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


        //
        listView = findViewById(R.id.listview_questions);
        adapter = new ArrayAdapter<QuestionItem>(this, android.R.layout.simple_list_item_1, arrayList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);


    }

    @Override
    protected void onStart() {
        super.onStart();

//        localhostDialog(); //
        loginDialog();

    }

    public void localhostDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.dialog_localhost, null);
        builder.setView(dialogView);

        final EditText localhost = dialogView.findViewById(R.id.edit_localhost);
        builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MyUtility.ipaddress = localhost.getText().toString();

                try {
                    URL url = new URL("http://"+MyUtility.ipaddress+"/skillstest42/db/getall.php");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    InputStream is = conn.getInputStream();
                    StringBuffer data = new StringBuffer();
                    int ch=0;
                    while ((ch=is.read())!=-1){
                        data.append((char)ch);
                    }

                    is.close();
                    conn.disconnect();
Toast.LENGTH_SHORT).show();

                    //..........

                        }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.show();
    }

    public void loginDialog(){
        AlertDialog.Builder sbuilder = new AlertDialog.Builder(this);
        LayoutInflater sinflater = this.getLayoutInflater();
        final View studentView = sinflater.inflate(R.layout.dialog_student, null);
        sbuilder.setView(studentView);

        final EditText stud_idno = studentView.findViewById(R.id.edit_idno);
        final EditText stud_passw = studentView.findViewById(R.id.edit_passw);

       //.....

        sbuilder.show();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

       //....

}

How can I possibly send a POST request from my android app? Thanks for any help that you can give in this scenario.

CoderUni
  • 5,474
  • 7
  • 26
  • 58
no_profile
  • 387
  • 5
  • 15

1 Answers1

1

you can use AsyncHttpClient library

https://loopj.com/android-async-http/

String url = "http://www.yourdomain/loginstudent.php";
RequestParams params = new RequestParams();
    params.put("username", YOURTEXT);
    params.put("password", lng);
    AsyncHttpClient client=new AsyncHttpClient();
    client.post(url, params, new TextHttpResponseHandler() {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            Log.d(TAG, "onFailure:CrachReporter " + throwable.toString());
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {


        }
    });
Mehrdad
  • 1,477
  • 15
  • 17