繁体   English   中英

从JSON获取名称:值对时出错

[英]Error with getting name:value pair from JSON

我正在尝试从json字符串中获取name:value对

,尽管出现错误。 我正在使用android Studio和java,并想采用该名称:值对,并将TextView设置为等于该值对。

我相信主要的问题是我可能错误地从JSON中获取了name:value对,并且我也可能错误地更改了TextView。

@Override
public void onResponse(Call call, Response response) throws IOException {
    Log.v("s","--onResponse successfully called.");

    String mmessage = response.body().string();
    Log.v("s", "--Response: " + mmessage);

    try {
        JSONObject json = new JSONObject(mmessage);
        String api_info_balance = json.getString("balance");
        String api_info_pending_charges = json.getString("pending_charges");
        String api_info_last_payment_date = json.getString("last_payment_date");
        String api_info_last_payment_amount = json.getString("last_payment_amount");

         _balancetextview.setText(api_info_balance.toString());
         _pending_chargestextview.setText(api_info_pending_charges.toString());
         _last_payment_datetextview.setText(api_info_last_payment_date.toString());
         _last_payment_amounttextview.setText(api_info_last_payment_amount.toString());
    }catch(Exception e) {
        e.printStackTrace();
        Log.v("s", e.toString());
    }
}

这是返回的json:

{“余额”:“-23.66”,“待付款项”:“ 0.00”,“最后付款日期”:“ 2017-01-27 14:52:13”,“最后付款金额”:“-50.00”}

logcat的:

    [ 11-11 15:34:08.755 16120:16186 D/         ]
                                                                                  SurfaceInterface::setAsyncMode: set async mode 1
11-11 15:34:08.762 16120-16186/com.example.sss.lesdothis D/EGL_emulation: eglMakeCurrent: 0x9c98f8a0: ver 2 0 (tinfo 0xa8a8b7e0)
11-11 15:34:20.640 16120-16120/com.example.sss.lesdothis V/s: --button clicked.
11-11 15:34:22.142 16120-16627/com.example.sss.lesdothis V/s: --onResponse successfully called.
11-11 15:34:22.142 16120-16627/com.example.sss.lesdothis V/s: --Response: {"balance":"-23.66","pending_charges":"0.00","last_payment_date":"2017-01-27 14:52:13","last_payment_amount":"-50.00"}
11-11 15:34:22.143 16120-16627/com.example.sss.lesdothis W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-11 15:34:22.144 16120-16627/com.example.sss.lesdothis W/System.err:     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7286)
11-11 15:34:22.144 16120-16627/com.example.sss.lesdothis W/System.err:     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1155)
11-11 15:34:22.144 16120-16627/com.example.sss.lesdothis W/System.err:     at android.view.View.requestLayout(View.java:21922)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis I/chatty: uid=10082(u0_a82) i.vultr.com/... identical 5 lines
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err:     at android.view.View.requestLayout(View.java:21922)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err:     at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:1959)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err:     at android.view.View.requestLayout(View.java:21922)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err:     at android.widget.TextView.checkForRelayout(TextView.java:8526)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err:     at android.widget.TextView.setText(TextView.java:5392)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err:     at android.widget.TextView.setText(TextView.java:5248)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err:     at android.widget.TextView.setText(TextView.java:5205)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err:     at com.example.sss.lesdothis.MainActivity$1$1.onResponse(MainActivity.java:113)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err:     at java.lang.Thread.run(Thread.java:764)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis V/s: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis V/s: --pgld1

您应该仅将ui线程中的ui更新为stacktrace状态。 如果您处于活动中,可以通过调用runOnUiThread方法来实现:

runOnUiThread(new Runnable() {
@Override
public void run() {
    _balancetextview.setText(api_info_balance.toString());
    _pending_chargestextview.setText(api_info_pending_charges.toString());
    _last_payment_datetextview.setText(api_info_last_payment_date.toString());
    _last_payment_amounttextview.setText(api_info_last_payment_amount.toString());
}
});

基本上,只需将您的UI更新包装在对此方法的调用中,它就不再崩溃。 如果您想了解更多,这里是文档链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM