Android : Problems with sending a string to the server in android?

on Saturday, September 27, 2014


Good day. It seems to be simple, there is a line json, you want to throw on the server, at what the server does not check the correctness construction line, or something else, just the server and just a string, but the problem is that the server is somehow always returns 0, it means that line, he has not entered. Here's where to send post request addef38885.nichost.ru/and.php?data=. So after all data that is already on the server = will go into a variable. My request is placed within the function and looks



static String SendJsonViaPost(String url, String json) {
String responseJson = "";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
try {
StringEntity stringEntity = new StringEntity(json);
stringEntity.setContentType("application/json");
post.setEntity(stringEntity);
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
responseJson += line;
}

} catch (IOException e) {
e.printStackTrace();
}

return responseJson;}


and here is how I call it:



SendJsonViaPost("http://addef38885.nichost.ru/and.php?data=","testing_file_1");


there is the PHP code of the server.



<?php

$par = $_POST['data'];

if ( strlen($par) >=1 )
{
$fp = fopen('data/'.date('Ymd_His').'.txt', "a"); // Открываем файл в режиме записи

$mytext = $par; // Исходная строка

$test = fwrite($fp, $mytext); // Запись в файл

if ($test) { echo '1'; } else { echo '0'; }

fclose($fp); //Закрытие файла
}
else { echo '0'; }

?>

0 comments:

Post a Comment