我似乎无法在网上找到最新的教程,因为其中大多数(例如 HttpClient)已被贬值。
我想要让用户使用他们的 ID 号和密码从我的 Android 应用程序登录。数据可以从我的Web MySql数据库中找到。
这是我的连接: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));
?>
从我的数据库获取学生信息的代码: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;
}
我的 Android 应用程序所做的是显示一个警报对话框并询问本地主机(已修复),然后显示另一个对话框,要求其登录。
这是我的 MainActivity.php 的 fragment
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) {
//....
}
我怎样才能从我的 Android 应用程序发送 POST 请求?感谢您在这种情况下提供的任何帮助。
最佳答案
您可以使用 AsyncHttpClient 库
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) {
}
});
关于java - 如何从 Android 向 PHP 发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57742664/