3

Hi! I'm developing an internet based app in android. What i want to do is send a user id and password to a php web server and return a response from the server. The response could be a text, like "valid" or "invalid", and if the response is "valid" then a new activity should be launched. I don't know how to send data to a PHP server from android and read a response from the server. The following PHP code will generate a proper response. Please help me regarding this as it is important in my final year project of my BS in computer science. Thanks!

<?php
 $user= $_POST["uid"];
 $pwd=$_POST["pass"];
 $con= mysql_connect("localhost","root");
 if(!$con)
 {
     die("Not able to connect");

 }
 mysql_select_db("try",$con);
 $result=mysql_query("Select * from info where uid='$user'and pass='$pwd'");

 if( mysql_num_rows($result)<=0)
 {
     echo "unsuccessful";
 }
 else
 {
     echo "successful";
 }
 mysql_close($con);
?>
Name is Nilay
  • 2,743
  • 4
  • 35
  • 77

3 Answers3

1

To send UID and Passto your PHP file::

List<NameValuePair> updatemsg = new ArrayList<NameValuePair>();
updatemsg.add(new BasicNameValuePair("uid", uidmsg));
updatemsg.add(new BasicNameValuePair("pass", passmsg));
ttpClient httpclient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); //Timeout Limit
HttpPost httppost = new HttpPost("your_url.php");
httppost.setEntity(new UrlEncodedFormEntity(updatemsg));
httpclient.execute(httppost);

And to get values from PHP file::

            HttpClient httpclient = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); //Timeout Limit
            HttpPost httppost = new HttpPost("your_url.php");
            httppost.setEntity(new UrlEncodedFormEntity(updatemsg));
            HttpResponse response = httpclient.execute(httppost);

            InputStream content = response.getEntity().getContent();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
            String s = "";
            while ((s = buffer.readLine()) != null) {

                //s is the returned value;

            }
Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
1

Try to use something like this:

<?
$sock = fsockopen("url.com", 80, $errno, $errstr, 30);
if ($sock)
 {
 fwrite($sock,  "GET /some_request HTTP/1.0\r\n" .
            "Host: url.com\r\n" .
            "\r\n");

 $beg = 0;
 while (!feof($sock))
  echo fread($sock, 128);

 fclose($sock);
 }
?>
Angrybambr
  • 220
  • 3
  • 10
  • actually my php code is fine but what i wanna know is that how can i send request from android to php and send echo message back to android – Mirza Hassam Nov 22 '11 at 07:22
  • Hm... I didn't understand your question. Look here: http://stackoverflow.com/questions/3505930/make-in-http-request-with-android – Angrybambr Nov 22 '11 at 07:50
1

From your app, do something like this:

    URL url = new URL("http://www.your-site.com/auth_user.php?username="+username+"&pwd="+pwd);        
    URLConnection authUserConn = url.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(authUserConn.getInputStream()));
    String inputLine;
    String res = "";
    if ((inputLine = in.readLine()) != null) 
        res += inputLine;
    in.close();
    //parse result
    Integer success = Integer.valueOf(res);
    // ...
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129