简体   繁体   中英

To upload Registration data from Android To PHP server

My PHP code is as follow

         <?php
         error_reporting(~E_NOTICE);
         //include('db.php');

        if($_GET['fname'])
           {
           $fname=$_GET['fname'];
           //echo $fname.'gytgy';
                 }
           if($_GET['lname'])
            {
          $lname=$_GET['lname'];
             //echo $lname.'vbvcbcvb';
            }
           if($_GET['email'])
          {
            $email=$_GET['email'];
           //echo $email.'fgfdgdf';
              }
          $conn=mysql_connect("localhost","MyserverDatabase username","MyserverDatabase password");
         if(!$conn)
              {
                die("could not connect". mysql_error());
                  }
             mysql_select_db("MyserverDatabase username",$conn);


          //$name =$_POST['name'];
         $query="insert into android
           (fname,lname,email)values('".$fname."','".$lname."','".$email."')";

          $query1=mysql_query($query);
            if ($query1="")
         {echo "unsuccessfull";
              }
              else
            {
         echo"successfull";
            }
               ?>

My android Java Code is As follow

      public class AUSRWC1 extends Activity {

EditText fname;
EditText lname;
EditText email;
InputStream is;
TextView text;
List<NameValuePair> nameValuePairs;
byte[] data;
StringBuffer buffer;
public static int a = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    fname = (EditText)findViewById(R.id.name1);
    lname = (EditText)findViewById(R.id.name2);
    email = (EditText)findViewById(R.id.emailname);
    text= (TextView)findViewById(R.id.text);
    Button register = (Button) findViewById(R.id.register);
    register.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {   
            if(fname.getText().length()== 0)
            {
                text.setText("Enter firstname");
            }
            else if(lname.getText().length()== 0)
            {
                text.setText("Enter Lastname");
            }
            else if(email.getText().length()== 0)
            {
                text.setText("Enter Email Id");
            }
            else{
                try{
                String a = fname.getText().toString().trim();
                String b = lname.getText().toString().trim();
                String c = email.getText().toString().trim();
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new
                HttpPost("http://android/android_user.php");
                nameValuePairs = new ArrayList<NameValuePair>(3);
                nameValuePairs.add(new BasicNameValuePair("fname", "parth"));
                nameValuePairs.add(new BasicNameValuePair("lname", "da"));
                nameValuePairs.add(new BasicNameValuePair("email", "asd"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_16));


                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                fname.setText("");
                lname.setText("");
                email.setText("");
                text.setText("");
                }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
                text.setText("Connection error");
                }
            }
        }
    });
 }
 }    
     //Also my Manifest I have Included Permission for Internet.

My code is not working @ when i enter my data on My emulator my data does not goes into my server database instead of that some blank data goes into database Can anyone please help me how can I do the same.Also Please point out what is the mistake in My code.

Error was on this line I feel:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_16));

I replaced this with,

UrlEncodedFormEntity URLEntity = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(URLEntity );

Did you try:

DefaultHttpClient client = new DefaultHttpClient();

instead of HttpClient client = new DefaultHttpClient();

And let me know

I would suggest you to check the status of your web call, you can check it through:

int status = response.getStatusLine().getStatusCode();

If you getting 200 as status code then its a successful web call otherwise something is going wrong.

@Parth Dani You have a error in the php file...You are actually making a POST call throug your program.
HttpPost httppost = new HttpPost("http://android/android_user.php");

and you are using GEt in your Php code ...what you have to do is just replace GET with POST in your Php code and you're done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM