user3345767
user3345767

Reputation: 349

Android json result null

hello everyone i try to parse json .i have simple json like thislike this

i try to parse this json but result is null .i have no idea what i'm doing wrong this is a my code

public class HomeActivity extends Activity {
private String URL = "*****************";
private TextView PNumber;

JSONArray jsonArray = null;

public static String KEY_PNumber = "PNumber";
String id;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    PNumber = (TextView) findViewById(R.id.PNumber);
    new LoadDataAllChanelsToServer().execute();
}

private class LoadDataAllChanelsToServer extends
        AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(String... urls) {

        JSONParser jParser = new JSONParser();


        JSONObject json = jParser.getJSONFromUrl(URL);

         try {

    JSONObject jsonObject = json.getJSONObject("data");

    id = jsonObject.getString("PNumber");
} catch (JSONException e) {
    e.printStackTrace();
}

        return null;

    }

    @Override
    protected void onPostExecute(String result) {
        PNumber.setText(id);
    }

}

}

what am i doing wrong if anyone knows solution help me thanks

i have this errror error occurred while executing doinbackground()

Upvotes: 0

Views: 58

Answers (1)

Nitish
Nitish

Reputation: 3155

The Json you posted does not contain any array. Its a JSON object. A Json array starts with [. You can read more about JSON from here.

Now, regarding parsing:

Do it as follows:

    try {

        JSONObject jsonObject = json.getJSONObject("data");

        String pNumber = jsonObject.getString("PNumber");
    } catch (JSONException e) {
        e.printStackTrace();
    }

Upvotes: 1

Related Questions