0

I am fetching json data for different book_name & its working fine.but in case if book does not exists my app get collapse.I know that JsonObject fetching null data but i want it to do something rather than collapse when book not found. My code is: `

   public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

    final String TAG = "AsyncTaskParseJson.java";

    // set your json string url here
   Intent po=getIntent();
    String bok=po.getStringExtra("bookname");
    String newName = bok.replaceAll("\\s", "%20");
    String yourJsonStringUrl = "http://www.bsservicess.com/photoUpload/star_avg.php?bookName="+newName;

    // contacts JSONArray
    JSONArray dataJsonArr = null;
    @Override
    protected void onPreExecute() {}


    protected String doInBackground(String... arg0) {
        if(exists(yourJsonStringUrl)) {
        try {
            JSONParser jParser = new JSONParser();

            // get json string from url

            JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);



                // get the array of users
                dataJsonArr = json.getJSONArray("result");
                JSONObject c = dataJsonArr.getJSONObject(0);
                //    na=c.getString("avg");

                ratenumS = c.getString("avg");
                numberS = c.getString("num");
                starts = Float.parseFloat(c.getString("avg"));


        } catch (JSONException e) {
            e.printStackTrace();
        }
        }else{
            starts=0;
            numberS="0";
        }

        return null;
    }

    @Override
    protected void onPostExecute(String strFromDoInBg) {
        super.onPostExecute(strFromDoInBg);
        ratenum.setText(ratenumS);
        number.setText("("+numberS+")");
        netRate.setRating(starts);
     //   Toast.makeText(mybookview.this, Float.toString(starts),Toast.LENGTH_SHORT).show();
    }

Plz help me. `

       public JSONObject getJSONFromUrl(String url) {

    // make HTTP request
    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();

    } catch (Exception e) {
        Log.e(TAG, "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e(TAG, "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
}

}`

2
  • can you post your JSONParser class? Commented Jul 15, 2016 at 18:43
  • okk i post in mty edit question Commented Jul 15, 2016 at 18:53

2 Answers 2

1
// get json string from url

JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
if(json == null){
    //url parsing didn't work
    return;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to handle the exception, add try catch where your app is crashing.

2 Comments

i know where it is crashing Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference at dataJsonArr = json.getJSONArray("result"); but dnt know how to correct it
Is json null after ...JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl); ? If yes then you need a null check before u try to get array

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.