如何从PHP服务器获取字符串到Android?

如何从PHP服务器获取字符串到Android?,php,android,server,postdata,Php,Android,Server,Postdata,我创建应用程序向服务器发送文本,并检查是否(text!=null)返回新的文本值 我的代码如下所示: 在PHP服务器中: $text = $_POST["text1"]; if($text != null){ echo "Contact1-----".$text."-----10h49 25/03/2016 at New York city"; } else{ echo ""; } 之前,我在服务器上保存一个文件,并将所有数据写入该文

我创建应用程序向服务器发送文本,并检查
是否(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();
}
我不知道为什么
答案

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

联系人1$text 10h49 2016年3月25日于纽约市

$text=$\u POST[“text1”]


离开Android部分,首先调试PHP代码。您没有发布数据,因此这肯定是错误的:

$text = $_POST["text1"];
改为使用$\u GET[“text1”]:

$text = $_GET["text1"];

尽管可以使用$\u请求,但在我看来,这是一种不好的做法,因为它会合并$\u GET、$\u POST和$\u COOKIE变量。

set
connection.setDoInput(true)

如果在浏览器中打开anyexample.com/main.php?text1=asd会发生什么?@Eduard7它不会显示任何内容。您认为PHP文件中会发生这种情况吗?尝试将$\u POST[“text1”]更改为$\u GET[“text1”],我尝试了
$GET[“text1”]和
$\u REQUEST['text1'],但我没有从服务器接收值。不是$GET,而是$\u GET。不要使用$\请求:谢谢@Dhavalkumar Solanki:我试过你的改变。在Android客户端中,我没有从服务器PHP接收任何值。我认为
getTextFromInputStream
类中存在一个问题,因为在该类获取文件中的值之前。当前,我直接从PHP服务器中的
echo
中获取值。您好,ioewi一些时间日志不应适用于Http方法,因此如果您能够调试然后调试并检查响应,我在一段时间之前也遇到了相同的问题,日志不应工作,但当我在textview中设置时,它可以工作,非常感谢。我不使用
TextView
,因为有很多问题。很简单,我想检查服务器中的
text
,服务器用
echo
新字符串
text
响应。我收到了这一行,并在我的应用程序中继续处理。感谢@Eduard7,我使用class
sendToServer(最终字符串文本)
发布了一行,并希望通过
echo
新字符串值从PHP服务器接收新字符串。收到新行后,我的申请将继续另一个过程。
$text = $_GET["text1"];