linkas
linkas

Reputation: 173

JSON can't get Int value from php

My problem is simple. I can't get the value of item from php and my android app crashes. My PHP:

<?php
$control["value"] = 0;
echo json_encode($control);
?>

My Java Android:

private static final String CONTROL_URL = "xxxxx.controller.php";
int control;

...

@Override
public void onClick(View v) {
    JSONParser jParser = new JSONParser();
    //it crash here
    JSONObject json = jParser.getJSONFromUrl(CONTROL_URL);

    try {
          control = json.getInt(CONTROL_URL);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

        if (control == 0) {
                   //Do something...
            }

When i tried to go to my php file on browser it shows me: {"value":0} . So could someone help me solve this problem? By the way, my url - xxxxx.controller.php isn't real, I use a real one for my app, but I don't want to show it.

Upvotes: 0

Views: 451

Answers (4)

kushan-
kushan-

Reputation: 168

your error log shows that you are making a network call on the main thread (NetworkOnMainThreadException), which android prohibits. You will have to move the call for getting the JSON to a separate thread. Using an Async Task will accomplish this.

Upvotes: 0

Embattled Swag
Embattled Swag

Reputation: 1469

When you're grabbing the integer from your JSONObject, the key you inputted is the url which doesn't make sense. Try changing it to

control = json.getInt("value");

Upvotes: 1

Joel
Joel

Reputation: 1140

Wouldn't json.getInt require "value" as the parameter not the URL? so json.getInt("value")

Upvotes: 1

arielcr
arielcr

Reputation: 1663

You can try this:

<?php
$control = array('value' => 0);
echo json_encode($control);
?>

Upvotes: 0

Related Questions