【问题标题】:Method to send strings on a PHP page在 PHP 页面上发送字符串的方法
【发布时间】:2017-10-07 14:44:28
【问题描述】:

请我尝试使用有关“如何将字符串发送到 PHP 页面”的不同教程。 我尝试过的这个似乎是学习一些东西的最佳解决方案。 该应用程序编译没有错误,但在我的设备上似乎没有向 php 页面发送任何内容。 我授予 AndroidManifest.xml 的权限

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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{

                GetText();
            }
            catch(Exception ex)
            {
                content.setText("url exeption!");
            }
        }
    });
}


public void GetText() throws UnsupportedEncodingException
{

    Name    = fname.getText().toString();
    Email   = email.getText().toString();
    Login   = login.getText().toString();
    Pass    = pass.getText().toString();



    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
    {

        URL url = new URL("http://thingsforcoins.com/receive/httppost.php");

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


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


        while((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        text = sb.toString();
    }
    catch(Exception ex)
    {

    }
    finally
    {
        try
        {

            reader.close();
        }
        catch(Exception ex) {}
    }

    content.setText(text);

}

}

这个应用程序读取四个edittext字段并将数据保存在应该发送到PHP页面的字符串中,然后显示数据。

【问题讨论】:

    标签: java android httprequest


    【解决方案1】:

    我认为你应该这样做:

            byte[] postData = data.getBytes(Charset.forName("UTF-8"));
            int postDataLength = postData.length;
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Charset", "utf-8");
            conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
            conn.setUseCaches(false);
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.write(postData);
            wr.close();
            if (conn.getResponseCode() == 200) {
                //...
            } else {
                //...
            }
    

    data 是你的String data = URLEncoder.encode("name", "UTF-8") + ... 也可能不需要conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");这行代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多