Reputation: 295
My code posts a json object to a server. This is my PHP
<?php
$jsonString = file_get_contents('php://input');
$jsonObj = json_decode($jsonString, true);
if( !empty($jsonObj)) {
$players = $jsonObj['Players'];
$details = $jsonObj['Details'];
$events = $jsonObj['Events'];
print "YES";
var_dump($players);
}else{
print "NO";
}
?>
Here is a snippet of the android code.
inputStream = httpResponse.getEntity().getContent();
String status = httpResponse.getStatusLine().toString();
if (!status.equals("HTTP/1.1 500 Internal Server Error")){
if(inputStream != null){
result = convertInputStreamToString(inputStream);
}
else{
result = "Did not work!";
}
}else{
System.out.println("500 Error");
}
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
System.out.println(e);
}
System.out.println(result);
return result;
The problem I am having is that when i run this code 'NO' is output on the PHP page but the response I get from server (result) shows 'YES' and outputs JSON.
Upvotes: 0
Views: 98
Reputation: 2567
I answered in comments, but @Tanis.7x said I should add an answer.
$jsonObj
is empty and you get 'NO' when you run code via browser (you don't pass any json). It seems you pass some json via android request.
You need preserve json data on a server if you want to show it in a browser.
$jsonString = file_get_contents('php://input');
$jsonObj = json_decode($jsonString, true);
$file = 'tmp';
if(!empty($jsonObj)) {
file_put_contents($file, $jsonString);
} else {
$content = file_get_contents($file);
if ($content) {
echo $content;
//$jsonObj = json_decode($content, true);
} else {
echo 'NO';
}
}
Upvotes: 1