2016-03-25 45 views
3

我創建應用程序發送文本到服務器,並檢查if(text != null)返回新的文本值。如何從PHP服務器獲取字符串到Android?

我的代碼要做到這一點,如:

在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

+0

如果您在瀏覽器中打開anyexample.com/main.php?text1=asd會發生什麼? – Eduard7

+0

@ Eduard7它沒有顯示任何東西。你認爲發生在PHP文件? – vanloc

+0

嘗試將$ _POST [「text1」]更改爲$ _GET [「text1」] – Eduard7

回答

1

先離開Android部分和調試你的PHP代碼。你不張貼的數據,所以這肯定是不對的:

$text = $_POST["text1"]; 

使用$ _GET [ 「text1」 中]代替:

$text = $_GET["text1"]; 

您可以使用$ _REQUEST,雖然,但國際海事組織這是一個壞練習,因爲它合併了$ _GET,$ _POST和$ _COOKIE變量。

+0

感謝@ Eduard7,我使用類sendToServer(最終字符串文本)'張貼一行並希望通過'echo'新字符串值從PHP服務器接收一個新字符串。 收到新的一行後,我的應用程序將繼續另一個過程。 – vanloc

1

更換

$text = $_POST["text1"];

$text = $_REQUEST['text1']; 
+0

謝謝@Dhavalkumar Solanki:我試過你的改變。在Android客戶端中,我沒有收到來自服務器PHP的任何值。我認爲'getTextFromInputStream'類中存在一個問題,因爲在這個類獲取文件中的值之前。目前,我直接從PHP服務器中的'echo'獲取值。 – vanloc

+0

嗨ioewi一些時間日誌不應該爲Http方法工作,所以如果你能夠調試然後調試和檢查響應,我有相同的問題之前一段時間,日誌不應該工作,但是當我在textview中設置它工作完美 –

+0

非常感謝你許多。我不使用'TextView'因爲很多問題。簡單來說,我想檢查服務器和服務器中的「文本」,並用'echo'新字符串'text'來響應。我收到這一行,並在我的申請繼續過程。 – vanloc

0

設置connection.setDoInput(true);

+0

感謝@mallaudin,我解決了我的問題。 – vanloc

相關問題