gpt4 book ai didi

java - 从键值 json 响应访问字符串

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

我收到这个简单的 json 响应,但无法轻松访问该值。回复是这样的

{"email":"ted@ted.com"}

那是在执行 json.toString() 之后。现在我试图访问电子邮件的值(value),但不断收到错误。我以为只是这样

json.getString("email")

这也是java中的。

编辑:这是我遇到的错误

07-22 06:45:11.524: E/AndroidRuntime(9977): FATAL EXCEPTION: AsyncTask #2
07-22 06:45:11.524: E/AndroidRuntime(9977): java.lang.RuntimeException: An error occured while executing doInBackground()
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-22 06:45:11.524: E/AndroidRuntime(9977): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-22 06:45:11.524: E/AndroidRuntime(9977): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-22 06:45:11.524: E/AndroidRuntime(9977): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-22 06:45:11.524: E/AndroidRuntime(9977): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-22 06:45:11.524: E/AndroidRuntime(9977): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-22 06:45:11.524: E/AndroidRuntime(9977): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-22 06:45:11.524: E/AndroidRuntime(9977): at java.lang.Thread.run(Thread.java:856)
07-22 06:45:11.524: E/AndroidRuntime(9977): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.view.View.invalidate(View.java:10193)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.widget.TextView.invalidateRegion(TextView.java:4375)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.widget.TextView.invalidateCursor(TextView.java:4318)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.widget.TextView.spanChange(TextView.java:7172)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:8759)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.text.Selection.setSelection(Selection.java:76)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.text.Selection.setSelection(Selection.java:87)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.widget.TextView.setText(TextView.java:3535)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.widget.TextView.setText(TextView.java:3405)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.widget.EditText.setText(EditText.java:80)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.widget.TextView.setText(TextView.java:3380)
07-22 06:45:11.524: E/AndroidRuntime(9977): at com.reflap.reflap.EditProfile$fillfields.doInBackground(EditProfile.java:61)
07-22 06:45:11.524: E/AndroidRuntime(9977): at com.reflap.reflap.EditProfile$fillfields.doInBackground(EditProfile.java:1)
07-22 06:45:11.524: E/AndroidRuntime(9977): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-22 06:45:11.524: E/AndroidRuntime(9977): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-22 06:45:11.524: E/AndroidRuntime(9977): ... 5 more

我在后台进程中执行此操作。

最佳答案

我相信您的堆栈跟踪不是由 JSON 数据的解析引起的,而是由您的 AsyncTask 引起的。正在引用 TextView 中的 UI(某些 doInBackground )方法。

您通常会引用您的观点onPostExecute ,可能通过WeakReference s。

关于java - 从键值 json 响应访问字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17785459/

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