Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将注册数据从Android上传到PHP服务器_Java_Php_Android - Fatal编程技术网

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

Java 将注册数据从Android上传到PHP服务器,java,php,android,Java,Php,Android,我的PHP代码如下 <?php error_reporting(~E_NOTICE); //include('db.php'); if($_GET['fname']) { $fname=$_GET['fname']; //echo $fname.'gytgy'; } if($_GET['lname'

我的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.
我的代码不起作用@当我在模拟器上输入数据时,我的数据不会进入服务器数据库,而是一些空白数据进入数据库。有人能帮我做同样的事情吗?另外,请指出我代码中的错误

您是否尝试过:

DefaultHttpClient客户端=新的DefaultHttpClient

而不是HttpClient=新的DefaultHttpClient


请让我知道

我建议您检查网络通话的状态,您可以通过以下方式进行检查:

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

如果您的状态码是200,那么这是一个成功的web调用,否则会出现问题。

@Parth Dani您的php文件中有一个错误……您实际上是在通过程序进行POST调用。 HttpPost HttpPost=新 HttpPosthttp://android/android_user.php;


您正在Php代码中使用GEt…您需要做的就是在Php代码中用POST替换GEt,您就完成了。

错误出现在这一行,我感觉:

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

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

谢谢你的回答,但我仍然在将空白数据输入我的数据库。你在服务器端使用请求后方法获取数据吗?是的,我在服务器端使用请求后方法获取数据。我甚至不知道PHP的基础知识。如果我从java的角度来看php代码,那么像fname、lname这样的变量似乎是If块的局部变量。在if块之后,这些变量被销毁。在将值保存到数据库时,正在获取空白值。如果我错了,也请告诉我PHP中的变量范围。我得到的一个方法是将我的Android代码替换为HttpClient HttpClient=new DefaultHttpClient;HttpPost HttpPost=新的HttpPost;HttpResponse response=httpclient.executehtppost;HttpEntity=response.getEntity;is=entity.getContent;但我不确定这种方式是否是编写的……我得到的一种方式是将我的Android代码替换为HttpClient HttpClient=new DefaultHttpClient;HttpPost HttpPost=新的HttpPost;HttpResponse response=httpclient.executehtppost;HttpEntity=response.getEntity;is=entity.getContent;但我不确定这种方式是否是书面的。。。