通过服务器上的android app http url方法发送数据

时间:2016-09-24 12:34:48

标签: php android

我已经创建了一个应用程序,我想建立一个登录表单和出席我也已经建立了一个网站,以增加出席的PHP和它的工作正常,现在我创建应用程序,但我无法通过应用程序发送数据在服务器上我也看到了一些教程并得到了这段代码。

public class MainActivity extends ActionBarActivity {


TextView content;
EditText fname, email, login, pass;
String Name, Email, Login, Pass; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    content=(TextView)findViewById( R.id.content );
    fname=(EditText)findViewById(R.id.name);
    email=(EditText)findViewById(R.id.email);
    login=(EditText)findViewById(R.id.loginname);
    pass=(EditText)findViewById(R.id.password);


    Button saveme=(Button)findViewById(R.id.save);

    saveme.setOnClickListener(new Button.OnClickListener(){

        public void onClick(View v)
        {
            try{

                     // CALL GetText method to make post method call
                    GetText();
             }
            catch(Exception ex)
             {
                content.setText(" url exeption! " );
             }
        }
    });  
}

    / / Create GetText Metod
    public  void  GetText()  throws  UnsupportedEncodingException
  {
    // Get user defined values
    Name = fname.getText().toString();
    Email   = email.getText().toString();
    Login   = login.getText().toString();
    Pass   = pass.getText().toString();

     // Create data variable for sent values to server  

      String data = URLEncoder.encode("name", "UTF-8") 
                   + "=" + URLEncoder.encode(Name, "UTF-8"); 

      data += "&" + URLEncoder.encode("email", "UTF-8") + "="
                  + URLEncoder.encode(Email, "UTF-8"); 

      data += "&" + URLEncoder.encode("user", "UTF-8") 
                  + "=" + URLEncoder.encode(Login, "UTF-8");

      data += "&" + URLEncoder.encode("pass", "UTF-8") 
                  + "=" + URLEncoder.encode(Pass, "UTF-8");

      String text = "";
      BufferedReader reader=null;

      // Send data 
    try
    { 

        // Defined URL  where to send data
        URL url = new URL("http://houseonclick.com/httppost.php");

     // Send POST data request

      URLConnection conn = url.openConnection(); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr = new   
    OutputStreamWriter(conn.getOutputStream()); 
      wr.write( data ); 
      wr.flush(); 

      // Get the server response 

    reader = new BufferedReader(new         
 InputStreamReader(conn.getInputStream()));
     StringBuilder sb = new StringBuilder();
    String line = null;

    // Read Server Response
    while((line = reader.readLine()) != null)
        {
               // Append server response in string
               sb.append(line + "\n");
        }


        text = sb.toString();
    }
    catch(Exception ex)
    {

    }
    finally
    {
        try
        {

            reader.close();
        }

        catch(Exception ex) {}
    }

    // Show response on activity
    content.setText( text  );

       }

   }

我在服务器上使用的php代码是

<?php 

   $name   = urldecode($_POST['name']);
   $user   = urldecode($_POST['user']);
   $email  = urldecode($_POST['email']);
   $pass   = urldecode($_POST['pass']);

   print " ==== POST DATA =====
   Name  : $name
   Email : $email
   User  : $user
   Pass  : $pass"; 

 ?>

这是我的xml文件代码

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"   
android:layout_height="wrap_content"
    android:stretchColumns="*" android:background="#ffffff">


 <TableRow android:background="#758AA7" android:layout_margin="2dip">
       <TextView android:id="@+id/output"
        android:layout_height="wrap_content"
        android:padding="1px"
        android:text="@string/default_output" 
   android:layout_width="wrap_content" />
       <TextView android:id="@+id/content"
        android:layout_height="wrap_content"
        android:padding="2px"
        android:text="@string/default_content"   
 android:layout_width="fill_parent" />
 </TableRow>
 <TableRow android:background="#758AA7" android:layout_margin="2dip">

    <Button android:text="Save On Web"
    android:id="@+id/save"
    android:layout_width="wrap_content" 
    android:layout_gravity="center" 
     />
    </TableRow>

 <TableRow android:background="#ffffff" android:layout_margin="2dip">

    <TextView style="@style/CodeFont"
     android:text="@string/Name"   android:layout_width="30px"   
  android:layout_gravity="left"/>
    <EditText android:id="@+id/name" 
    android:layout_width="wrap_content"
         android:gravity="left" android:layout_gravity="left"  android:width="210px"/>
</TableRow>
 <TableRow android:background="#ffffff" android:layout_margin="2dip">

    <TextView style="@style/CodeFont"
     android:text="@string/Email"   android:layout_gravity="left"/>
    <EditText android:id="@+id/email" 
     android:layout_width="wrap_content" android:layout_gravity="left" 
  android:width="210px"/>
  </TableRow>
   <TableRow android:background="#ffffff" android:layout_margin="2dip">

    <TextView style="@style/CodeFont"
     android:text="@string/LoginName"   android:layout_gravity="left"/>
    <EditText android:id="@+id/loginname" 
     android:layout_width="wrap_content" android:layout_gravity="left"   
   android:width="210px"/>
</TableRow>
 <TableRow android:background="#ffffff" android:layout_margin="2dip">

    <TextView style="@style/CodeFont"
     android:text="@string/Password"   android:layout_gravity="left"/>
    <EditText android:id="@+id/password" 
     android:layout_width="wrap_content" android:layout_gravity="left"   
   android:width="210px"/>
   </TableRow>
  </TableLayout>   

有人可以告诉我这里有什么问题,以及如何发送和获取数据,如果出现问题,我在此感谢做了什么改变。

0 个答案:

没有答案