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

Ave
  • 4,338
  • 4
  • 40
  • 67

3 Answers3

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.

Eduard7
  • 725
  • 7
  • 19
  • 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. – Ave Mar 26 '16 at 03:46
1

Replace

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

$text = $_REQUEST['text1'];
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
  • Thanks @Dhavalkumar Solanki: I tried your change. In Android client, I don't receive any values from server PHP. I think a problem in `getTextFromInputStream`class because before this class get values in a file. Current, I get values direct from `echo` in PHP server. – Ave Mar 25 '16 at 09:52
  • 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 – Dhaval Solanki Mar 25 '16 at 09:55
  • 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. – Ave Mar 26 '16 at 03:40
0

set connection.setDoInput(true);

mallaudin
  • 4,744
  • 3
  • 36
  • 68