我似乎无法在网上找到最近的教程,因为它们中的大多数(例如HttpClient)都被贬低了。
这是我的联系方式:登录学生.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));
?>
从我的数据库中获取学生信息的代码:数据库连接.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应用程序所做的是显示一个alertdialog,请求localhost(已经修复),然后显示另一个对话框,请求他们登录。
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请求?感谢您在此场景中提供的任何帮助。