【问题标题】:Parsing isn't working - Android解析不起作用 - Android
【发布时间】:2026-01-17 12:15:01
【问题描述】:

我有以下解析类用于在数据库函数中进行搜索,但解析不起作用。 “无法解析”吐司正在显示。 另外 logcat 没有显示任何错误,所以我无法指定哪个部分导致吐司显示。

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;
    }
}

日志

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

【问题讨论】:

  • 在 parse() 中抛出异常,请发布堆栈跟踪
  • 堆栈跟踪是什么?
  • 它是所有被调用到导致异常的代码行的方法的列表。它将出现在 logcat 中,您可以做的一件事是在您的解析函数中注释 try{} catch{} 以便异常会导致崩溃,因此您会看到堆栈跟踪为红色
  • JSONException:值已经属于类型。错误出现在以下几行: public class Parser extends AsyncTask { return this.parse(); JSONArray ja = new JSONArray(data);
  • 请将整个问题复制粘贴到您的问题中,例如他们在this question中所做的那样

标签: android parsing


【解决方案1】:

什么时候调用 Parser 构造函数?在调用异步任务之前,变量 data、ctx 和 listView 是否已初始化?如果是这样,那么尝试在当前 try 块中记录一些消息,因为似乎当前 try 块中的语句甚至没有执行,它直接跳转到返回 0。

【讨论】:

  • 我添加了 Log.d("Joudi", e.getMessage());但 logcat 上没有显示任何内容
  • 然后检查您的Android日志监视器,看看是否有任何异常。我认为您的 parse 函数引发了一些异常。
  • JSONException:值已经属于类型。错误出现在以下几行: public class Parser extends AsyncTask { return this.parse(); JSONArray ja = new JSONArray(data);
  • 你能提到你的数据变量的值吗?它包含什么?
  • 是的,这就是我在回答中提到的。检查它是否已初始化。请点赞。谢谢:)