【问题标题】:Problem in Iteraiton of a JsonOBJECT: Is impossible to iterate with a whileJsonOBJECT 迭代中的问题:不可能迭代一段时间
【发布时间】:2021-09-13 12:19:26
【问题描述】:

我想在 Json Objet 中进行迭代。我的 Json 文件是:

    [
  {
    "id": 2,
    "tagw2": "flemme",
    "conseil2": "travailler"
  },
  {
    "id": 3,
    "tagw": "flemme",
    "conseil": "travailler"
  }
]

但是当我尝试迭代时,我的 android 应用程序总是发送一个空的 toast。我这样做是为了。这是我的代码:

Button GetData;
GetData = (Button) findViewById(R.id.GetData);
GetData.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {   
// Instantiate the RequestQueue.
  RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
    String url = " myserverJsonIPs";
   JsonArrayRequest request=new JsonArrayRequest(Request.Method.GET,url, null,new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        String GiveTagw = "";
                        String GiveConseil= "";
                        String testo;
                        try {
                            //JSONObject Info = response.getJSONObject(0);
                            for (int i=0;i<4;i++)
                            {
                                JSONObject Info = response.getJSONObject(i);
                                Toast.makeText(getApplicationContext(), GiveTagw, Toast.LENGTH_SHORT).show();
                                //testo = Info.getString(String.valueOf(id));
                                //GiveTagw = Arrays.asList(Info.getString("tagw").split("\\s*,\\s*"));
                                GiveTagw = Info.getString("tagw");

                                Toast.makeText(getApplicationContext(), GiveTagw , Toast.LENGTH_SHORT).show();

                                GiveConseil = Info.getString("conseil");
                                //testMap4.put(Arrays.asList(Info.getString("tagw").split("\\s*,\\s*")), GiveConseil);
                                //id = id + 1;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        mTextViewResult.setText(GiveTagw);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        mTextViewResult.setText("something wrong");
                    }
                });

                queue.add(request);

            }
        });

当我点击我创建的按钮时,我没有“出错”错误消息,但只有一个空的 Textview 和一个空的 Toast...我很困惑

当我点击按钮时,我唯一能运行的是:

 W/System.err:     at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
W/libEGL: EGLNativeWindowType 0x6ee583fc90 disconnect failed
D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
W/libEGL: EGLNativeWindowType 0x6ee583fc90 disconnect failed
D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
W/libEGL: EGLNativeWindowType 0x6ee583fc90 disconnect failed
D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
W/libEGL: EGLNativeWindowType 0x6ee583fc90 disconnect failed
D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
W/libEGL: EGLNativeWindowType 0x6ee583fc90 disconnect failed

新的日志运行:

V/AudioManager: querySoundEffectsEnabled...
W/System.err: org.json.JSONException: No value for tagw
        at org.json.JSONObject.get(JSONObject.java:399)
        at org.json.JSONObject.getString(JSONObject.java:560)
W/System.err:     at fr.apprentissage.version2.MainActivity$2$1.onResponse(MainActivity.java:112)
        at fr.apprentissage.version2.MainActivity$2$1.onResponse(MainActivity.java:104)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
W/System.err:     at android.os.Looper.loop(Looper.java:213)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

LogRun3

    V/AudioManager: querySoundEffectsEnabled...
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/AwareBitmapCacher: handleInit switch not opened pid=3109
W/System.err: org.json.JSONException: No value for tagw
        at org.json.JSONObject.get(JSONObject.java:399)
W/System.err:     at org.json.JSONObject.getString(JSONObject.java:560)
        at fr.apprentissage.version2.MainActivity$2$1.onResponse(MainActivity.java:112)
        at fr.apprentissage.version2.MainActivity$2$1.onResponse(MainActivity.java:104)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
W/System.err:     at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

日志运行 3

        W/Settings: Setting device_provisioned has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
V/HiTouch_HiTouchSensor: User setup is finished.
V/AudioManager: querySoundEffectsEnabled...
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/System.err: org.json.JSONException: No value for tagw
        at org.json.JSONObject.get(JSONObject.java:399)
        at org.json.JSONObject.getString(JSONObject.java:560)
        at fr.apprentissage.version2.MainActivity$2$1.onResponse(MainActivity.java:113)
        at fr.apprentissage.version2.MainActivity$2$1.onResponse(MainActivity.java:105)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
D/AwareBitmapCacher: handleInit switch not opened pid=6482
V/AudioManager: querySoundEffectsEnabled...
I/HwViewRootImpl: removeInvalidNode all the node in jank list is out of time
W/System.err: org.json.JSONException: No value for tagw
W/System.err:     at org.json.JSONObject.get(JSONObject.java:399)
        at org.json.JSONObject.getString(JSONObject.java:560)
        at fr.apprentissage.version2.MainActivity$2$1.onResponse(MainActivity.java:113)
W/System.err:     at fr.apprentissage.version2.MainActivity$2$1.onResponse(MainActivity.java:105)
W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

【问题讨论】:

  • 检查是否有异常。
  • 我编辑了我的帖子以添加运行日志,我不想这样做:(
  • 打印catch块中异常信息的日志。
  • 您能否详细说明如何打印此异常消息日志?对不起,有时我理解的很慢......
  • 在 catch 块中添加行 "Log.d("===exce :: "+e.getMessage());"

标签: java android json rest loops


【解决方案1】:

你在后台线程中运行 toast,尝试在主线程中移动 toast。

// read obj1
JSONObject objResult1 = response.getJSONObject(0);
int id1 = objResult1.optInt("id");
String tagw1 = objResult1.optString("tagw2");
String conseil1 = objResult1.optString("conseil2");

// read obj2
JSONObject objResult2 = response.getJSONObject(1);
int id2 = objResult2.optInt("id");
String tagw2 = objResult2.optString("tagw");
String conseil2 = objResult2.optString("conseil");


new Handler(Looper.getMainLooper()).post(() -> {
    Toast.makeText(getApplicationContext(), "id1:" + id1 + " tagw1:" + tagw1 + " conseil1"  + conseil1  , Toast.LENGTH_LONG).show();
    Toast.makeText(getApplicationContext(), "id2:" + id2 + " tagw2:" + tagw2 + " conseil2"  + conseil2  , Toast.LENGTH_LONG).show();
});

【讨论】:

  • 我试图添加你在for循环中发布的处理程序,但我认为这不是你认为我必须做的。我必须在主要添加这个?我不能将它添加到函数外部的 main 中,因为 GiveTagw 是一个局部变量,我错了吗?对不起我的困难。
【解决方案2】:

试试下面的代码可能对你有帮助。

   public void onResponse(JSONArray response) {
    String tagw = "";
    String giveConseil = "";
    try {
        for (int i = 0; i < response.length(); i++) {
            JSONObject info = response.getJSONObject(i);
            int id = info.getInt("id");
            if (info.has("tagw"))
                tagw = info.getString("tagw");
            if (info.has("tagw2"))
                tagw = info.getString("tagw2");

            if (info.has("conseil"))
                giveConseil = info.getString("conseil");
            if (info.has("conseil2"))
                giveConseil = info.getString("conseil2");

            Toast.makeText(getApplicationContext(), giveConseil, Toast.LENGTH_SHORT).show();
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
 }

【讨论】:

  • 我试图用你的代码替换我的代码。 json 服务器正确地看到了“get”请求”,但我的应用程序没有在 toast 中打印任何东西......我试图用 mTextViewResult.setText("test"); 替换它,但是 textview 没有改变并且“ text" 没有出现在我的应用中
  • 尝试像这样在日志中打印 Log.d("TAG",tagw):
  • 好的,我添加运行日志并添加 Log.d 编辑我的帖子(“W/System.err: org.json.JSONException: No value for tagw”????)
  • 我再次更新了我的帖子检查,并确保您的 json 对象键正是“tagw”
  • 我有同样的日志运行错误我添加它编辑我的帖子。在您编辑后,我用您的代码复制并替换了我的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 2012-04-27
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多