cannot access echoed data from localhost using android

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


Source: wampserver

Leave a Reply