Mohammad Sohaib
Mohammad Sohaib

Reputation: 575

Send data from Android to Webservice to be inserted in my database

I'm trying to send a few variables(or data) for registration to a webservice from android throught "httppost" method. Here's the code.

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsitename.com/webservice/register.php");
try {
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);

nameValuePairs.add(new BasicNameValuePair("fname", et3.getText().toString()));
 nameValuePairs.add(new BasicNameValuePair("lname", et4.getText().toString()));
 nameValuePairs.add(new BasicNameValuePair("userid", et5.getText().toString()));
 nameValuePairs.add(new BasicNameValuePair("pass", et6.getText().toString()));
 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 httpclient.execute(httppost);

 } catch (ClientProtocolException e) {
                         // TODO Auto-generated catch block
 } catch (IOException e) {
                         // TODO Auto-generated catch block
}

Following is the Register.php file code.

<?php
$host = "localhost"; 
$user = "myusername"; 
$pass = "mypass";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$userid = $_POST['userid'];
$pass = $_POST['pass'];

$db_select=mysql_select_db("the_db");
if(!$db_select){
    die(mysql_error());
    echo "error";
}

$query= "INSERT INTO USERS(fname, lname, userid, pass)
VALUES ('{$fname}', '{$lname}', '{$userid}', '{$pass}'); " ;

if($medo=mysql_query($query)){
    header("localhost/filename");
    exit;
}else{
    echo"<p> Error</p>";
    die(mysql_error());
}

When I try to run it, gives me an error. It says on my device that "YourApp has stopped working". Possibly a nullpointer exception or something like that. I don't know if this is the correct way to send simple variables like names and passwords etc. to the register.php. Can you tell me what the problem is here?

Upvotes: 1

Views: 5988

Answers (1)

britzl
britzl

Reputation: 10242

Please have a look at the LogCat output. It should say at what line in which class you got an exception. Post the logcat output here if you need help. I would also recommend that you take a look at the Android Asynchronous Http Client. It greatly simplifies HTTP communication. Example of an HTTP POST:

AsyncHttpClient client = new AsyncHttpClient();
RequestParams rp = new RequestParams();
rp.put("fname", et3.getText().toString());
rp.put("lname", et4.getText().toString());
rp.put("userid", et5.getText().toString());
rp.put("pass", et6.getText().toString());

client.post("http://mywebsitename.com/webservice/register.php", rp, new AsyncHttpResponseHandler() {
    @Override
    public final void onSuccess(String response) {
        // handle your response here
    }

    @Override
    public void onFailure(Throwable e, String response) {
        // something went wrong
    }               
});

Upvotes: 3

Related Questions