Je crée l’application pour envoyer un texte au serveur et vérifier if(text != null)
renvoie de nouvelles valeurs de texte.
Mon code pour le faire comme:
En PHP Server:
$text = $_POST["text1"]; if($text != null){ echo "Contact1-----".$text."-----10h49 25/03/2016 at New York city"; } else{ echo ""; }
Avant, je sauvegarde un fichier sur le serveur et écris toutes les données sur ce fichier. À ce moment, je veux qu’il revienne aux données Android, pas besoin d’enregistrer pour classer. Et dans le code Android est:
final Ssortingng scripturlssortingng = "http://www.anyexample.com/main.php"; AddContactActivity contact; public void sendToServer(final Ssortingng text){ contact = new AddContactActivity(); new Thread(new Runnable() { @Override public void run() { try { Ssortingng textparam = "text1=" + URLEncoder.encode(text, "UTF-8"); URL scripturl = new URL(scripturlssortingng); 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 Ssortingng answer = getTextFromInputStream(answerInputStream); if(answer!="") { Ssortingng[] 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 Ssortingng getTextFromInputStream(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); SsortingngBuilder ssortingngBuilder = new SsortingngBuilder(); Ssortingng currentLine; try { while ((currentLine = reader.readLine()) != null) { ssortingngBuilder.append(currentLine); ssortingngBuilder.append("\n"); } } catch (IOException e) { e.printStackTrace(); } return ssortingngBuilder.toSsortingng().sortingm(); }
Je ne sais pas pourquoi la answer
est null
.
Je pense qu’il doit retourner les données comme:
Contact1 $text 10h49 25/03/2016 at New York city
Laissez la partie Android et commencez par déboguer votre code PHP. Vous ne postez pas les données, donc c’est vraiment faux:
$text = $_POST["text1"];
Utilisez plutôt $ _GET [“text1”]:
$text = $_GET["text1"];
Vous pouvez cependant utiliser $ _REQUEST, mais IMO est une mauvaise pratique, car il fusionne les variables $ _GET, $ _POST et $ _COOKIE.
Remplacer
$text = $_POST["text1"];
avec
$text = $_REQUEST['text1'];
set connection.setDoInput(true);