简体   繁体   English

将注册数据从Android上传到PHP服务器

[英]To upload Registration data from Android To PHP server

My PHP code is as follow 我的PHP代码如下

         <?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 我的Android Java代码如下

      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(); DefaultHttpClient客户端=新的DefaultHttpClient();

instead of HttpClient client = new DefaultHttpClient(); 而不是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. 如果您获得200作为状态代码,则说明它是成功的Web呼叫,否则出问题了。

@Parth Dani You have a error in the php file...You are actually making a POST call throug your program. @Parth Dani您的php文件中有一个错误...您实际上正在通过程序进行POST调用。
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. 并且您在Php代码中使用GEt ...您所要做的就是在您的Php代码中将GET替换为POST即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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