1

I am pulling my hair out over this. I still don't fully understand how JSON works. I am trying to return a number (formatted as a string) from a PHP script to java. I keep getting the following error:

Error parsing data org.json.JSONException: Value http of type java.lang.String cannot be converted to JSONArray

This is a snippet from the PHP code:

class result
{
 public $value = "";
}
$result = new result();
$result->value = "1";
print(json_encode($result));

This returns: {"value":"1"}

I am trying to store the '1' in a string for error checking purposes on the android side.

It inserts into MySQL database first and that works but it wont return the value correctly.

Here is the java code I have been attempting to use:

try 
{
 CustomHttpClient.executeHttpPost(response="http://test.com/test.php",postParameters);
 String result = response.toString();
 try
 {
 returnString = "";
 JSONArray jArray = new JSONArray(result);
 for(int i=0;i<jArray.length();i++)
 {
 JSONObject json_data = jArray.getJSONObject(i);
 Log.i("log_tag","value:"+json_data.getString("value"));
 returnString = json_data.getString("value");
 }
 }
 catch(JSONException e)
 {
 Log.e("log_tag", "Error parsing data "+e.toString());
 Toast.makeText(getApplicationContext(), "Error Parsing", Toast.LENGTH_LONG).show(); 
 }
}
stefana
2,6364 gold badges33 silver badges48 bronze badges
asked Feb 25, 2015 at 13:37
2
  • 1
    well do you mean you want to pass json using php to java? Commented Feb 25, 2015 at 13:38
  • 1
    you´re trying to read an array but it´s a single object. Commented Feb 25, 2015 at 13:45

3 Answers 3

2

Your PHP code returns a JSON object (enclosed in {}), not an array (enclosed in []). So, you need to change your JSON parsing code as

returnString = "";
JSONObject json_data = new JSONObject(result);
Log.i("log_tag","value:"+json_data.getString("value"));
returnString = json_data.getString("value");

Since, there's only one JSON object being returned, there's no need of the loop too.

answered Feb 25, 2015 at 13:51
Sign up to request clarification or add additional context in comments.

Comments

0

First of all I ́d recomend you to use Gson API which is an amazing API for handling JSON objects. I ́d strongly recomend you to read the guides at their website.

The problem here is that you ́re trying to convert an object into an array and that ́s why you ́re getting an Exception. The correct way to convert the value you specified above is:

JsonObject jObject = (JsonObject) new JsonParser().parse(value);

The example above is using Gson API but the same applies to your API.

answered Feb 25, 2015 at 14:00

Comments

0

You can try with this example, this is work for me:

public String listOfUser() {
 //User users = new User();
 String URI = "http://localhost/Perumahan/index.php/User_c";
 RestTemplate restTemplate = new RestTemplate();
 HttpHeaders headers = new HttpHeaders();
 headers.setContentType(MediaType.APPLICATION_JSON);
 List<LinkedHashMap> users = restTemplate.getForObject(URI, List.class);
 //userService.addUser(users);
 return gson.toJson(users);
 }
answered May 20, 2016 at 20:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.