gpt4 book ai didi

java - 获取名称时出错 :value pair from JSON

转载 作者:行者123 更新时间:2023-12-02 12:01:19 25 4
gpt4 key购买 nike

我正在尝试从 json 字符串中获取名称:值对

,尽管我遇到了错误。我正在使用 android Studio 和 java,并且想要获取该名称:值对并将 TextView 设置为等于该值对。

我认为主要问题是我可能错误地从 JSON 中获取名称:值对,而且我也可能错误地更改了 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:

{"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: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。如果您处于 Activity 中,可以通过调用 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 更新包装在对此方法的调用中,它就不会再崩溃了。这是link to the docs如果你想了解更多。

关于java - 获取名称时出错 :value pair from JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47239718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com