【问题标题】:How to get string from PHP server to Android?如何从 PHP 服务器获取字符串到 Android?
【发布时间】:2016-07-13 01:26:59
【问题描述】:

我创建应用程序以向服务器发送文本并检查 if(text != null) 返回新的文本值。

我的代码如下:

在 PHP 服务器中:

$text = $_POST["text1"];
if($text != null){
    echo "Contact1-----".$text."-----10h49 25/03/2016 at New York city";
} else{
    echo "";
}                      

之前,我在服务器上保存了一个文件,并将所有数据写入该文件。 这时候,我希望它返回到android数据,不需要保存到文件。 而在Android代码中是:

final String scripturlstring = "http://www.anyexample.com/main.php";

AddContactActivity contact;
public void sendToServer(final String text){
    contact = new AddContactActivity();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {

                String textparam = "text1=" + URLEncoder.encode(text, "UTF-8");

                URL scripturl = new URL(scripturlstring);
                HttpURLConnection connection = (HttpURLConnection) scripturl.openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setFixedLengthStreamingMode(textparam.getBytes().length);
                contentWriter.write(textparam);
                contentWriter.flush();
                contentWriter.close();

                InputStream answerInputStream = connection.getInputStream();
                final String answer = getTextFromInputStream(answerInputStream);

                if(answer!="") {
                    String[] contactInfo = answer.split("-----");

                    contact.insertContact(contactInfo[0], contactInfo[1], contactInfo[2], "", "");
                }
                answerInputStream.close();
                connection.disconnect();


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

}



public String getTextFromInputStream(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder stringBuilder = new StringBuilder();

    String currentLine;
    try {
        while ((currentLine = reader.readLine()) != null) {
            stringBuilder.append(currentLine);
            stringBuilder.append("\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return stringBuilder.toString().trim();
}

我不知道为什么answernull

我认为它必须返回如下数据:

Contact1 $text 10h49 25/03/2016 at New York city

【问题讨论】:

  • 如果您在浏览器中打开 anyexample.com/main.php?text1=asd 会发生什么?
  • @Eduard7 它没有显示任何内容。你认为发生在 PHP 文件中吗?
  • 尝试将 $_POST["text1"] 更改为 $_GET["text1"]
  • 我尝试使用 $GET["text1"] and $_REQUEST['text1'] 但我没有收到来自服务器的值。
  • 不是 $GET 而是 $_GET。不要使用 $_REQUEST:stackoverflow.com/questions/1924939/…

标签: php android server postdata


【解决方案1】:

替换

$text = $_POST["text1"];

$text = $_REQUEST['text1'];

【讨论】:

  • 谢谢@Dhavalkumar Solanki:我试过你的改变。在 Android 客户端中,我没有收到来自服务器 PHP 的任何值。我认为getTextFromInputStreamclass 存在问题,因为在此类之前获取文件中的值。目前,我直接从 PHP 服务器中的echo 获取值。
  • 嗨 ioewi 一些时间日志不应该适用于 Http 方法,所以如果你能够调试然后调试并检查响应,我在一段时间之前遇到了同样的问题,日志不应该工作,但是当我在 textview 中设置时它工作完美
  • 非常感谢。我不使用TextView,因为有很多问题。很简单,我想在服务器中检查text,服务器响应echo新字符串text。我收到此行并在我的申请中继续处理。
【解决方案2】:

离开 Android 部分并首先调试您的 PHP 代码。你没有发布数据,所以这绝对是错误的:

$text = $_POST["text1"];

改用 $_GET["text1"]:

$text = $_GET["text1"];

您可以使用 $_REQUEST,但 IMO 这是一个不好的做法,因为它合并了 $_GET、$_POST 和 $_COOKIE 变量。

【讨论】:

  • 感谢@Eduard7,我使用sendToServer(final String text) 类发布一行,并希望通过echo 新字符串值从PHP 服务器接收一个新字符串。收到新线路后,我的申请将继续另一个流程。
【解决方案3】:

设置connection.setDoInput(true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多