簡體   English   中英

將注冊數據從Android上傳到PHP服務器

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

我的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";
            }
               ?>

我的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.

當我在模擬器上輸入數據時,我的代碼不起作用@我的數據沒有進入我的服務器數據庫,而是一些空白數據進入了數據庫。有人可以幫助我該怎么做。也請指出是什么我的代碼中的錯誤。

我覺得這條線上有錯誤:

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

我換成了

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

你試過了嗎:

DefaultHttpClient客戶端=新的DefaultHttpClient();

而不是HttpClient client = new DefaultHttpClient();

讓我知道

我建議您檢查網絡通話的狀態,可以通過以下方式進行檢查:

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

如果您獲得200作為狀態代碼,則說明它是成功的Web呼叫,否則出問題了。

@Parth Dani您的php文件中有一個錯誤...您實際上正在通過程序進行POST調用。
HttpPost httppost = new HttpPost("http://android/android_user.php");

並且您在Php代碼中使用GEt ...您所要做的就是在您的Php代碼中將GET替換為POST即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM