【发布时间】: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