donglun1020 2015-08-03 08:36
浏览 89

无法使用android访问localhost的回显数据

I can succesfully update data (register a user for example) on my localhost wamp server with my android app, but can't retrieve and display data from the localhost in my logcat. Here is the asynctask I use in android

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())); 
}
}

Here here is my login.php file where I only try to send the usercredentials to my android app to confirm that the user exists on the 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);
?>

When I try

new AsyncLogIn(this, 1).execute("some_email", "some_password"); //1 means   login

I get the following

08-03 00:50:59.423 8386-8386/com.openx.opencity D/log in result is: 0

Basically, the result variable returns nothing. Anyone know what the problem is? Note that I have succesfully registered users with:

new AsyncLogIn(this, 0).execute('some-email', 'some_password', 'some_username');

so at least I can connect to the server

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据