使用curl制作android登录表单

j2qf4p5b  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(356)

我需要帮助,使登录使用 curl ,我只是试图使它,但它总是失败。我是否需要php文件?如果是的话,你能给我举一个如何使用它的例子吗。
这里是我的网址,使登录使用nikèu巴鲁和密码
http://hrd.tvip.co.id/rest_server/api/login/index
这是我的密码

package com.example.eis2;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    EditText editTextnik_baru;
    EditText editTextpassword;
    Button buttonlogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextnik_baru = (EditText) findViewById(R.id.editTextnik_baru);
        editTextpassword = (EditText) findViewById(R.id.editTextpassword);
        buttonlogin = (Button) findViewById(R.id.buttonlogin);

        buttonlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!editTextnik_baru.getText().equals("")){
                    sendLogin();

                } else {
                    editTextnik_baru.setError("Please insert NIK");
                    editTextpassword.setError("Please insert password");
                }
            }
        });
    }
    private void sendLogin() {
        String url = "http://hrd.tvip.co.id/rest_server/api/login/index";
        url += editTextnik_baru.getText();
        final String nik_baru = editTextnik_baru.getText().toString().trim();

        StringRequest loginRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray array = new JSONArray("data");
                            JSONObject json = array.getJSONObject(0);
                            String status = json.getString("nik_baru");
                            System.out.println("data "+ status);
//                            JSONObject json = getJSONObject("data");
                            if(status.equals(nik_baru)){
                                Log.d("tvip", "success");
                                Intent intent = new Intent(MainActivity.this, menu.class);
                                startActivity(intent);
                                finish();
                            }else{
                                Toast.makeText(getApplicationContext(), "Username & Password Salah", Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
                    }
                }
        ){
            @Override
            protected Map<String, String> getParams(){
                HashMap<String, String> params = new HashMap<>();
                params.put("nik_baru", editTextnik_baru.getText().toString());
                params.put("password", editTextpassword.getText().toString());
                return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(loginRequest);
    }
}

这是它的xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00d0ff"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/logoeis"
        android:layout_width="148dp"
        android:layout_height="140dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        app:srcCompat="@drawable/logoeis" />
    <ImageView
        android:id="@+id/logoasa"
        android:layout_width="38dp"
        android:layout_height="43dp"
        android:layout_below="@+id/logoeis"
        android:layout_marginLeft="300dp"
        android:layout_marginTop="-200dp"
        app:srcCompat="@drawable/logoasa" />
    <ImageView
        android:id="@+id/logotvip"
        android:layout_width="38dp"
        android:layout_height="43dp"
        android:layout_below="@+id/logoeis"
        android:layout_marginLeft="250dp"
        android:layout_marginTop="-200dp"
        app:srcCompat="@drawable/logotvip" />
    <EditText
        android:id="@+id/editTextnik_baru"
        android:layout_width="241dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_below="@+id/logoeis"
        android:layout_centerHorizontal="true"
        android:hint="NIK Baru"
        android:inputType="text"
        android:textColor="#000000"
        android:selectAllOnFocus="true"/>
    <EditText
        android:id="@+id/editTextpassword"
        android:layout_width="241dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextnik_baru"
        android:layout_centerHorizontal="true"
        android:hint="Password"
        android:textColor="#000000"
        android:inputType="textPassword"
        android:selectAllOnFocus="true"/>
    <Button
        android:id="@+id/buttonlogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextpassword"
        android:layout_centerHorizontal="true"
        android:text="Login"
        android:layout_marginTop="15dp" />
    <TextView
        android:id="@+id/eis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Employee Information System"
        android:textSize="23dp"
        android:layout_marginTop="12dp"
        android:textColor="#000000"
        android:layout_below="@+id/buttonlogin"
        android:layout_centerHorizontal="true"/>
    <TextView
        android:id="@+id/version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Version 1.0"
        android:textSize="23dp"
        android:layout_marginTop="12dp"
        android:textColor="#000000"
        android:layout_below="@+id/eis"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

你们能解决这个问题吗?

nqwrtyyt

nqwrtyyt1#

这只是一个例子。。。特定登录系统的确切编码可能不同。
服务器登录文件 PHP 类似于:

if (isset($_POST['username']) && $_POST['username'] != '') {
    $loginPassword = $_POST['password'];
    $username = $_POST['username'];
    $sql = "SELECT `id`, `username`, `email`, `password`, `key` FROM `TABLE_NAME` WHERE `username`=:username";
    $LoginRS_query = $conn->prepare($sql);
    $LoginRS_query->bindValue(':username', $username, PDO::PARAM_STR);
    $LoginRS_query->execute();
    $LoginRS = $LoginRS_query->fetch(PDO::FETCH_ASSOC);
    $totalRows = $LoginRS_query->rowCount();

    if($totalRows > 0) {
        $db_key = $LoginRS['key'];
        $peppered = hash_hmac("sha256", $loginPassword, $db_key);
        $db_pwd = $LoginRS['password'];
        if(password_verify($peppered, $db_pwd)) {
            $loginFoundUser = 'true';
        } else {
            $loginFoundUser = 'false';
        }
    }

    if($loginFoundUser == 'true') {
        $loginID =  $LoginRS['id'];
        $loginUserName = $LoginRS['username'];

        $LoginRS_query->closeCursor();

        echo "success";

        $response = array('id' => $loginID, 'username' => $loginUserName);
        echo json_encode($response);
    } else {
        $LoginRS_query->closeCursor();
        echo "no user found";
    }
} else {
    echo "access failed";
}

然后在你的活动文件里面 sendLogin() ...

public void onResponse(String response) {
    if (response.contains("success")) {
        String res = response.substring(response.indexOf("{"));
        try {
            JSONObject jsonObject = new JSONObject(res);
            final String loginID = jsonObject.getString("id");
            final String loginUser = jsonObject.getString("username");

            Intent intent = new Intent(MainActivity.this, menu.class);
            intent.putExtra("id", loginID); // added this line
            intent.putExtra("username", loginUser); // added this line
            startActivity(intent);
            finish();
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Login Error " + e, Toast.LENGTH_LONG).show();
        }
    }else{
        Toast.makeText(getApplicationContext(), "Username & Password Salah", Toast.LENGTH_LONG).show();
    }

这个 intent.putExtra 只有在执行其他任务时需要用户名或id时才需要
希望这有帮助。。。快乐编码:)

相关问题