大约一年前,我有一个需要登录功能的应用程序,我遵循了将PHP用于Android应用程序以与MySQL数据库对话的指南。效果很好,我用Oreo(API 26和27)在手机上很好地完成了任务。
但是现在我需要类似的功能才能在新项目中再次工作,这些应用程序无法在Android Pie(API 28)上运行。
Login.java
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class Login extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText et_log, et_pw;
final Button bt_log;
et_log = (EditText) findViewById(R.id.et_login);
et_pw = (EditText) findViewById(R.id.et_password);
bt_log = (Button) findViewById(R.id.bt_login);
bt_log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String ID = et_log.getText().toString();
final String PW = et_pw.getText().toString();
Response.Listener<String> respondslistener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonresponse = new JSONObject(response);
boolean success = jsonresponse.getBoolean("success");
if(success){
String Name = jsonresponse.getString("name");
String Position = jsonresponse.getString("position");
Intent intent = new Intent(Login.this, MainActivity.class);
intent.putExtra("name",Name);
intent.putExtra("position",Position);
intent.putExtra("ID",ID);
Login.this.startActivity(intent);
finish();
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setMessage("Login failed")
.setNegativeButton("Retry",null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Loginrequest loginrequest = new Loginrequest(ID,PW, respondslistener);
RequestQueue queue = Volley.newRequestQueue(Login.this);
queue.add(loginrequest);
}
});
}
}
Loginrequest.java
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class Loginrequest extends StringRequest {
private static final String LOGIN_REQUEST_URL ="http://private-ip/login.php";
private Map<String, String> params;
public Loginrequest(String ID, String PW, Response.Listener<String> listener){
super(Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("ID",ID);
params.put("PW",PW);
}
@Override
public Map<String, String> getParams() {
return params;
}
和login.php
将返回类似的内容
{"success":true,"name":"Jeff","position":"IT"}
是否可以使其在Pie上运行?我需要更改什么?