4

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

6
  • what happens if you open anyexample.com/main.php?text1=asd in the browser? Commented Mar 25, 2016 at 9:38
  • @Eduard7 It doesn't show anything. Do you think occurs in PHP file? Commented Mar 25, 2016 at 9:43
  • try to change $_POST["text1"] to $_GET["text1"] Commented Mar 25, 2016 at 9:46
  • I tried with $GET["text1"] and $_REQUEST['text1'] but I don't receive values from server. Commented Mar 25, 2016 at 9:54
  • not $GET but $_GET. don't use $_REQUEST: stackoverflow.com/questions/1924939/… Commented Mar 25, 2016 at 9:58

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to @Eduard7, I using class sendToServer(final String text) posting a line and want to receive a new string from PHP server through echo new string values. After having received a new line, my application will continue another process.
1

Replace

$text = $_POST["text1"]; with

$text = $_REQUEST['text1'];

3 Comments

Thanks @Dhavalkumar Solanki: I tried your change. In Android client, I don't receive any values from server PHP. I think a problem in getTextFromInputStreamclass because before this class get values in a file. Current, I get values direct from echo in PHP server.
Hi ioewi some time log should not work for Http method so if you able to debug then debug and check response, i have got same issue before some time, Log should not work but when i set in textview it work perfect
thank you very much. I don't use TextView because many problems. Simple, I want to check text in server and server responds with echo new string text. I receive this line and at to my application to continues process.
0

set connection.setDoInput(true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.