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);
?>
4
  • 1
    you should ask your tutor about sql injections & your database needs a pw for root user.... Commented Nov 22, 2011 at 7:12
  • possible duplicate of how do perform Http GET in android? Commented Nov 22, 2011 at 7:51
  • Why you are posting same question, in some other way. You are just confusing people. Commented Nov 22, 2011 at 8:32
  • Because im not getting my desired responses Commented Nov 24, 2011 at 5:00

3 Answers 3

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);
 }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

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
Hm... I didn't understand your question. Look here: stackoverflow.com/questions/3505930/…
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);
    // ...

Comments

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;

            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.