0

I have the following parse class for a search in the database function, but parsing isn't working. "Unable to parse" toast is showing. Plus logcat isn't showing any errors, so I can't specify which part is causing the toast to show.

public class Parser extends AsyncTask <Void, Void,Integer> {

    Context ctx;
    String data;
    ListView listView;
    ArrayList<String> titles = new ArrayList<>();


    public Parser (Context ctx, String data, ListView listView){
        this.ctx=ctx;
        this.data=data;
        this.listView=listView;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return this.parse();
    }

    @Override
    protected void onPostExecute(Integer integer) {
        super.onPostExecute(integer);

        if (integer == 1){

            ArrayAdapter adapter = new ArrayAdapter(ctx, android.R.layout.simple_list_item_1, titles);
            listView.setAdapter(adapter);
        }else {
            Toast.makeText(ctx, "Unable to Parse", Toast.LENGTH_LONG).show();
        }
    }

    private int parse (){
        try {
            JSONArray ja = new JSONArray(data);
            JSONObject jo = null;

            titles.clear();

            for (int i = 0; i < ja.length(); i++){
                jo=ja.getJSONObject(i);
                String title = jo.getString("post_title");
                titles.add(title);
            }

            return  1;
        } catch (JSONException e) {
            e.printStackTrace();

        }

        return 0;
    }
}

log

03-17 19:09:53.246 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: ViewPostImeInputStage processPointer 0
03-17 19:09:53.306 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: ViewPostImeInputStage processPointer 1
03-17 19:09:53.446 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 96 - 0, 1128) vi=Rect(0, 96 - 0, 1128) or=1
03-17 19:09:54.296 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{145fd86 V.E...... R.....I. 0,0-0,0}
03-17 19:09:54.306 13705-14599/com.example.joudialfattal.skillsexchange I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-17 19:09:54.306 13705-14599/com.example.joudialfattal.skillsexchange I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-17 19:09:54.306 13705-13705/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-17 19:09:54.326 13705-13733/com.example.joudialfattal.skillsexchange D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1592x909]-format:1
03-17 19:09:54.336 13705-13705/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-17 19:09:54.376 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
03-17 19:09:54.616 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: #3 mView = null
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err: org.json.JSONException: End of input at character 0 of 
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:92)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at com.example.joudialfattal.skillsexchange.Parser.parse(Parser.java:58)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at com.example.joudialfattal.skillsexchange.Parser.doInBackground(Parser.java:39)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at com.example.joudialfattal.skillsexchange.Parser.doInBackground(Parser.java:18)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
03-17 19:09:54.636 13705-14614/com.example.joudialfattal.skillsexchange W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-17 19:09:54.696 13705-13705/com.example.joudialfattal.skillsexchange E/ViewRootImpl: sendUserActionEvent() mView == null
03-17 19:09:54.706 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: #1 mView = android.widget.LinearLayout{864f1d1 V.E...... ......I. 0,0-0,0}
03-17 19:09:54.766 13705-13733/com.example.joudialfattal.skillsexchange D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [637x181]-format:1
03-17 19:09:54.776 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
03-17 19:09:54.776 13705-13705/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-17 19:09:54.806 13705-13733/com.example.joudialfattal.skillsexchange V/RenderScript: 0x7f690b1000 Launching thread(s), CPUs 8
03-17 19:09:58.206 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: #3 mView = null
03-17 19:10:28.296 13705-13705/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-17 19:10:28.346 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection
03-17 19:10:28.516 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection
03-17 19:10:28.606 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
03-17 19:10:28.606 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
03-17 19:10:28.606 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
03-17 19:10:28.676 13705-13705/com.example.joudialfattal.skillsexchange V/ActivityThread: updateVisibility : ActivityRecord{6fcff37 token=android.os.BinderProxy@267e99f {com.example.joudialfattal.skillsexchange/com.example.joudialfattal.skillsexchange.FindSkill}} show : true
03-17 19:10:28.716 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
03-17 19:10:28.716 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
03-17 19:10:28.716 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
03-17 19:10:28.716 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
03-17 19:10:28.716 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
03-17 19:10:28.716 13705-13705/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
03-17 19:10:29.176 13705-13705/com.example.joudialfattal.skillsexchange D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
user233531
  • 311
  • 1
  • 4
  • 17
  • 1
    an exception gets thrown in parse(), please post the stacktrace – lelloman Mar 17 '17 at 15:22
  • what is the stacktrace? – user233531 Mar 17 '17 at 15:40
  • it's the list of all the methods that had been called up to the the line of code that caused the exception. it will appear in the logcat, one thing you can do is comment try{} catch{} in your parse function so that the exception will cause a crash, so you will see the stacktrace in red – lelloman Mar 17 '17 at 15:42
  • JSONException: Value already of type. errors are on these lines: public class Parser extends AsyncTask { return this.parse(); JSONArray ja = new JSONArray(data); – user233531 Mar 17 '17 at 15:45
  • copy paste the whole thing in your question please, for example like they did in [this question](http://stackoverflow.com/questions/31574433/android-nullpointerexception-on-searchview-in-fragment) – lelloman Mar 17 '17 at 15:53
  • can you please go see the other comment and replies? – user233531 Mar 17 '17 at 16:01
  • just post the stacktrace, the cause of the error is there! – lelloman Mar 17 '17 at 16:10
  • I did you can check now. – user233531 Mar 17 '17 at 16:13
  • it's crashing at line 58 of your file, if it's this one `JSONArray ja = new JSONArray(data);` it means that the value of `data` is not a valid json – lelloman Mar 17 '17 at 16:15
  • yes it is that one. data is defined as string. what do you mean by a valid json? – user233531 Mar 17 '17 at 16:21

1 Answers1

0

When is your Parser constructor called? Are the variables data, ctx and listView initialized before calling the Async Task? If so, then try logging some message in the current try block because it seems that the statements inside the current try block are not even executed and it straightaway jumps to return 0.

Kamesh
  • 1,122
  • 9
  • 12
  • I added Log.d("Joudi", e.getMessage()); but nothing showed on logcat – user233531 Mar 17 '17 at 15:41
  • Then check your Android log monitor to see if there are any exceptions. I think your parse function throws some exception. – Kamesh Mar 17 '17 at 15:43
  • JSONException: Value already of type. errors are on these lines: public class Parser extends AsyncTask { return this.parse(); JSONArray ja = new JSONArray(data); – user233531 Mar 17 '17 at 15:47
  • 1
    Can you mention the value of your data variable? What does it contains? – Kamesh Mar 17 '17 at 15:50
  • I am following a tutorial that suggests this code. I just noticed that data isn't initialised and it is passed to JSONArray ja = new JSONArray(data);! – user233531 Mar 17 '17 at 15:55
  • 1
    Yes, that's what I mentioned in my answer. To check if it's initialized or not. Please upvote. Thanks :) – Kamesh Mar 17 '17 at 15:57
  • thank you. Can you please tell me how to initialise it? – user233531 Mar 17 '17 at 16:00
  • Just create an object of your Parser class from where you want to call the Async task by passing it the required parameters. Parser parser = new Parser(getApplicationContext(), [{"post_title" : "Android"}, {"post_title":"Apple"}], listview) – Kamesh Mar 17 '17 at 16:09
  • I have these in the main activity: searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { SenderReceiver sr = new SenderReceiver(FindSkill.this, urlAdress, query, listView,noData,noNetwork); sr.execute(); return false; } and same for onQueryTextChange – user233531 Mar 17 '17 at 16:14
  • Which tutorial are you following? Can you paste the link over here? – Kamesh Mar 17 '17 at 16:16