JsonOBJECT迭代中的问题:不可能用while迭代



我想在Json对象中迭代。我的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)

LOGRUN 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)

您在后台线程中运行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();
});

尝试使用以下代码可能会对您有所帮助。

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

最新更新