0

OK, few days ago I wrote a block of code in Java that sends post requests to a PHP file in order to store some data in a MySQL database and receive back simple json_encode() strings such as "error_101" responses from PHP and it worked just fine. Yesterday I reinstalled my XAMPP because I've had some problems with openssl PHP extention and now none of my json_encode() reponses return a value. I've checked the phpinfo() and it says that json support is enabled. To mention that values sent to PHP from JAVA are JSON objects as well and the json_decode() works just fine!

Here's my code to send responses from PHP to JAVA:

<?php 
    header('Content-type: application/json');
    echo json_encode("error_101");
?>

Here's the code to get the response in JAVA

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
HttpClient client = new DefaultHttpClient(httpParams);

String url = "http://192.168.254.19/android/register.php";

HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
request.setHeader("json", json.toString());

HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();

String result = null;
if (entity != null) {
    InputStream instream = entity.getContent();

    InputStreamReader is_reader = new InputStreamReader(instream);
    BufferedReader br = new BufferedReader(is_reader);
    result = br.readLine();
    Log.i("Read from server", result);
    Toast.makeText(this,  result, Toast.LENGTH_LONG).show();
}

The response I'm getting is "<br />"

2
  • Have you turned on error reporting? That <br> looks like a silenced error to me. Commented May 16, 2012 at 11:34
  • You say, "Yesterday I reinstalled my XAMPP ..." That may well mean that some settings got changed back to their default. Might be useful to check them all. Commented May 16, 2012 at 16:00

3 Answers 3

1

You sure you don't have some debug code somewhere up the chain that reads

echo $TesttVar1 . '<br />';

That would also stop the "header()" from working. Turn on ALL errors (error_reporting(E_ALL); ini_set('display_errors', 'on'); ) and that will show you the line the
is output, if that's the case.

But to help weed it out if it is json_encode, just return "Error_101" without the function to test. But I don't think you're getting that far down the program.

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

1 Comment

I've checked the code and I don't have any 'echo' statements at all except three echo json_encode(value) statements inside different if else statement blocks.
0

json_encode needs an array. like

json_encode(array('status'=>'error_101'));

5 Comments

You should be able to json_encode a string - you'd get {"Error_101"}. It's just most commonly used with arrays. (Correction - as pointed below, it would return "Error_101" - thanks prehfeldt)
@Robbie: that's not true either! If you encode a string to json you get "error_101" for example. See the third diagram on this page: json.org
Yep - sorry. Rushed typing. But the point it you can json_encode almost anything.
It should work with simple strings as well with int values. I've used responses like json_encode(101) and it worked.
@prehfeldt yes it is the case with using a simple string it just prints the string only. using array('str') returns it as ['str']
0

in this case:

header("Content-type: text/html");
echo json_encode("error_101");

it works.

in this other case:

header("Content-type: application/json");
echo json_encode("error_101");

it doesn't work.

It seems a bug!

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.