我如何将数据从Android发送到服务器并在Android中显示输出


How I Send Data From Android to Server and Show the Output in Android

我有一个简单的项目,但我不知道为什么输出不显示结果。

我从http://androidexample.com/How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=64&aaid=89

得到代码

我希望输出像图3那样在那个网页上

这是Java代码

public class MainActivity extends Activity {
TextView content;
EditText fname, email, login, pass;
String Name, Email, Login, Pass;
@Override
protected 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! " );
             }
        }
    });  
}
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://angkotfinder.16mb.com/cobaserver.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  );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是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"; 
?>

1。你不能在UI线程中做web请求。谷歌AsyncTask。

2。记得在你的AndroidManifest.xml中请求互联网权限,像这样:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />