Search code examples
phpandroidserverpostdata

How to get string from PHP server to Android?


I create the application to send a text to the server and check if(text != null) return new text values.

My code to do this like:

In PHP Server:

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

Before, I save a file on the server and write all data to this file. At this time, I want it to return back to android data, don't need to save to file. And in Android code is:

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();
}

I don't know why answer is null.

I think it must return the data like:

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


Solution

  • Leave the Android part and debug your PHP code first. You're not posting the data, so this is definitely wrong:

    $text = $_POST["text1"];
    

    Use $_GET["text1"] instead:

    $text = $_GET["text1"];
    

    You can use $_REQUEST though, but IMO it's a bad practice, as it merges down the $_GET, $_POST and $_COOKIE variables.