我正在创建一个具有登录屏幕的Android应用程序。用户必须输入他们的电子邮件地址和密码。当点击登录按钮时,我的代码应该使用post变量将输入的用户名和密码发送到php文件中。 php文件应该使用从应用程序发送的变量来完成select语句,如果有任何匹配,则从服务器上的数据库返回结果。 运行应用程序时,从php文件返回的输出是空白的。 我不知道post变量是否正确发送到php文件,我在互联网上搜索但似乎无法理解这个概念是如何工作的。
以下是我的代码:
package com.example.csqa;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast;
public class AsyncHttpPost extends AsyncTask<String, String, String> {
TextView t;
MainActivity ma;
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
public AsyncHttpPost(MainActivity m, List<NameValuePair> nvp){
ma = m;
nameValuePair = nvp;
}
@Override
protected String doInBackground(String... params) {
byte[] result = null;
String str = "";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);// in this case, params[0] is URL
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
result = EntityUtils.toByteArray(response.getEntity());
str = new String(result, "UTF-8");
}
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (Exception e) {
}
return str;
}
@Override
protected void onPostExecute(String output) {
// do something with the string returned earlier
Toast.makeText(ma.getApplicationContext(), "Calling validateUser with " + output, Toast.LENGTH_SHORT).show();
ma.validateUser(output);
}
}
这是PHP脚本
<?php
$con=mysqli_connect("127.0.0.1","S831987","831987",'D831987");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$email = $_POST['email'];
$password = $_POST['password'];
$result = mysqli_query($con,"SELECT * FROM USERS where
EMAIL='$email' and PASSWORD ='$password'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
echo $data;
}
mysqli_close($con);
?>
这是当用户按下登录按钮时从主活动执行的代码:
public void btnLoginClicked(View v){
TextView em = (TextView) findViewById(R.id.tvLogEmail);
TextView pw = (TextView) findViewById(R.id.tvLogPassword);
String email = em.getText().toString();
String password = pw.getText().toString();
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("email", email));
nameValuePair.add(new BasicNameValuePair("password", password));
AsyncHttpPost async = new AsyncHttpPost(this, nameValuePair);
async.execute("http://lamp.ms.wits.ac.za/~831987/isValidLogin.php");
}
有人可以解决我收到空白回复的原因吗? 我按原样测试了sql查询,使用了正确的变量,它可以正常工作。 提前谢谢!
答案 0 :(得分:0)
为什么不将ServiceHandler与json对象一起使用?这就是它对我有用的方式:
private class AsyncHttpPost extends AsyncTask<String, Void, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(getActivity(), "", "checking email and password...");
}
@Override
protected Void doInBackground(String... arg) {
String mEmail = arg[0]; // email entered by user
String mPassword = arg[1] // password entered by user
// Preparing post params
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", mEmail));
params.add(new BasicNameValuePair("password", mPassword));
ServiceHandler serviceClient = new ServiceHandler();
String json = serviceClient.makeServiceCall(YOUR_URL, ServiceHandler.POST, params);
Log.d("SERVER Request: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
boolean error = jsonObj.getBoolean("error");
// checking for error node in json
if (!error) {
// username and password checked successfully
} else {
Log.e("Server Error: ", "> " + jsonObj.getString("message"));
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "JSON data error!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
}
致谢: