0

Wondering if its way to check if the json url is null? by this I mean if url returning null and not json array/objects.

is there some way I can check it?

I have following code and I really wondering how I should do it.

class LoadAllGB extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(ListViser.this);
        mProgressDialog.setMessage("Vent Venligst..");
        mProgressDialog.setTitle("Henter");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    protected String doInBackground(String... args) {
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        String dato = df.format(Calendar.getInstance().getTime());

        DateFormat df2 = new SimpleDateFormat("HH");
        String klok = df2.format(Calendar.getInstance().getTime());

        String url = "http://192.168.1.15";
        Log.w("leyth",  "Leyth" + url);
        JSONArray json = jParser.getJSONFromUrl(url);
        try {
            //listen tømmes
            for(int i = 0; i < json.length(); i++){
                    JSONObject c = json.getJSONObject(i);

                String time = c.getString(Tag_time);
                String cover = c.getString(Tag_cover);
                String artist = c.getString(Tag_artist);
                String title = c.getString(Tag_title);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(Tag_time, time);
                map.put(Tag_cover, cover);
                map.put(Tag_artist, artist);
                map.put(Tag_title, title);

                abcList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
                       Toast.LENGTH_LONG).show();  
            Log.e("JSON Parser fejl", "fejl da man prøve og hente data " + e.toString());
        }

        return null;
    }

    protected void onPostExecute(String file_url) { 

        if(file_url!=null)//Do your validation for result
        {
            Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
                       Toast.LENGTH_LONG).show();  
        }
        if(mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }
        // Locate the listview in listview_main.xml
        adapter = new ListViewAdapter(ListViser.this, abcList);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        registerForContextMenu(listview);
    }
}
2
  • 1
    you are doing this in wrong way...you are not returning any file_url in your doInBackground(You are returning null),while you are accessing String file_url in onPost and checking on this that is already null.... Commented Jul 14, 2014 at 4:25
  • for jsonArray...if(json.length == 0){//jsonArrayEmpty}....and if(c.toString.isEmpty){//Then Json object null}.... Commented Jul 14, 2014 at 4:30

1 Answer 1

1

Try this way,hope this will help you to solve your problem.

   protected String doInBackground(String... args) {
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        String dato = df.format(Calendar.getInstance().getTime());

        DateFormat df2 = new SimpleDateFormat("HH");
        String klok = df2.format(Calendar.getInstance().getTime());

        String url = "http://192.168.1.15";
        Log.w("leyth", "Leyth" + url);
        if(jParser.getJSONFromUrl(url)!=null && (jParser.getJSONFromUrl(url) instanceof JSONArray || jParser.getJSONFromUrl(url) instanceof JSONObject)) {
            if (jParser.getJSONFromUrl(url) instanceof JSONArray && ((JSONArray) jParser.getJSONFromUrl(url)).toString().length() <= 0) {
                return "";
            } else if (jParser.getJSONFromUrl(url) instanceof JSONObject && ((JSONObject) jParser.getJSONFromUrl(url)).toString().length() <= 0) {
                return "";
            } else if(jParser.getJSONFromUrl(url) instanceof JSONArray && ((JSONArray) jParser.getJSONFromUrl(url)).toString().length() > 0){
                try {
                    JSONArray json = jParser.getJSONFromUrl(url);

                    //listen tømmes
                    for (int i = 0; i < json.length(); i++) {
                        JSONObject c = json.getJSONObject(i);

                        String time = c.getString(Tag_time);
                        String cover = c.getString(Tag_cover);
                        String artist = c.getString(Tag_artist);
                        String title = c.getString(Tag_title);

                        HashMap<String, String> map = new HashMap<String, String>();

                        map.put(Tag_time, time);
                        map.put(Tag_cover, cover);
                        map.put(Tag_artist, artist);
                        map.put(Tag_title, title);

                        abcList.add(map);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
                            Toast.LENGTH_LONG).show();
                    Log.e("JSON Parser fejl", "fejl da man prøve og hente data " + e.toString());
                }
                return String.valueOf(abcList.size());
            }else{

                try {
                    JSONObject json = jParser.getJSONFromUrl(url);

                    String time = c.getString(Tag_time);
                    String cover = c.getString(Tag_cover);
                    String artist = c.getString(Tag_artist);
                    String title = c.getString(Tag_title);

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(Tag_time, time);
                    map.put(Tag_cover, cover);
                    map.put(Tag_artist, artist);
                    map.put(Tag_title, title);

                    abcList.add(map);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
                            Toast.LENGTH_LONG).show();
                    Log.e("JSON Parser fejl", "fejl da man prøve og hente data " + e.toString());
                }
                return String.valueOf(abcList.size());
            }
        }else{
            return null;
        }
    }

    protected void onPostExecute(String file_url) {

        if(file_url!=null && file_url.length()>0)
        {
            // this is toast message for list with data
            Toast.makeText(getApplicationContext(), "this is my Toast message 1!!! =)",
                    Toast.LENGTH_LONG).show();
        }else if(file_url!=null && file_url.length()<=0){
            // this is toast message for empty list
            Toast.makeText(getApplicationContext(), "this is my Toast message 2!!! =)",
                    Toast.LENGTH_LONG).show();
        }else{
            // this is toast message for respone null
            Toast.makeText(getApplicationContext(), "this is my Toast message 3 !!! =)",
                    Toast.LENGTH_LONG).show();
        }
        if(mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }
        // Locate the listview in listview_main.xml
        adapter = new ListViewAdapter(ListViser.this, abcList);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        registerForContextMenu(listview);
    }
Sign up to request clarification or add additional context in comments.

5 Comments

You don't really need this check I guess : jParser.getJSONFromUrl(url) instanceof JSONObject You're anyways going to convert it to JSONArray which'll generate an exception.
i just gave alternative way to check and what you think i didn't know about it ?
Woah there tiger. Relax. Good for you if you knew.
Well its like how I wanted. no crash when there is no "json array/obj". But is there a way I can show a toast message when its empty?
@Neotrix Just add the toast message inside an else block of this if block : if(jParser.getJSONFromUrl(url)!=null && (jParser.getJSONFromUrl(url) instanceof JSONArray || jParser.getJSONFromUrl(url) instanceof JSONObject)){ ... }

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.