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

标签 java php 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 请求。谷歌异步任务。

2.请记住在您的 AndroidManifest.xml 中请求互联网权限,如下所示:

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

关于java - 我如何将数据从 Android 发送到服务器并在 Android 中显示输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30472089/

相关文章:

java - Reducer 中的 ArrayList 问题

java - Android 中 Web API 的异常处理

java - 如何在不影响对象的情况下将对象作为参数传递给方法?

java - 随机但很可能是 1 个 float

php - 在 PHP 5.3.3 上使用 Laravel 4 会遇到什么问题?

java - 您可以放入 Eclipse java 文档中的代码行数是否有限制

php - 如何在 ubuntu 后台运行多个 php 脚本?

javascript - 如何在 Vue.Js 中使用 $http.post 发送文件

android - requestFocus() 返回 false

java - Android单元测试: Instrumentation failed due to a class error