我可以使用我的Android应用程序在我的localhost wamp服务器上成功更新数据(例如注册用户),但无法从我的logcat中的localhost检索和显示数据。这是我在android中使用的asynctask
public class AsyncLogIn extends AsyncTask<String,Void,String>{
private static final String TAG = AsyncLogIn.TAG;
ProgressDialog progressDialog;
private Context context;
private int SignUpOrLogIn = 0;
String email=null;
String password=null;
String username=null;
//flag 0 means register and 1 means login
public AsyncLogIn(Context context, int flag) {
this.context = context;
SignUpOrLogIn = flag;
}
@Override
protected String doInBackground(String... arg0) {
String data=null;
email = (String)arg0[0];
password = (String)arg0[1];
String link=null;
try {
data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if(SignUpOrLogIn==0){
username=(String)arg0[2];
link="http://10.0.0.16/OpenX/register.php";
if(data!=null){
try{
data+= "&" + URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
else{
link="http://10.0.0.16/OpenX/login.php";
}
try {
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
} catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
Log.d("log in result is ", String.valueOf(result.length()));
}
}
这里是我的login.php文件,我只尝试将usercredentials发送到我的Android应用程序,以确认用户是否存在于db
<?php
$con=mysqli_connect("localhost","root","", "testdb");
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 email FROM `testdb`.`user` where email='$email' and password='$password'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
echo $data;}
else{
echo "WRONG";}
mysqli_close($con);
?>
当我尝试
时new AsyncLogIn(this, 1).execute("some_email", "some_password"); //1 means login
我得到以下
08-03 00:50:59.423 8386-8386 / com.openx.opencity D /登录结果为:0
基本上,结果变量不返回任何内容。谁知道问题是什么?请注意,我已成功注册用户:
new AsyncLogIn(this, 0).execute('some-email', 'some_password', 'some_username');
所以至少我可以连接到服务器