gpt4 book ai didi

android - java.lang.String 类型的值在传递 http 响应时无法转换为 JSONObject

转载 作者:行者123 更新时间:2023-11-29 22:39:58 24 4
gpt4 key购买 nike

我在尝试将响应字符串传递给 JSON 对象时遇到“java.lang.String 类型的值无法转换为 JSONObject”错误。我过去曾尝试过类似的方法,并且有效,所以我不知道为什么会这样。

这是代码

public void searchMovie(){
OkHttpClient client = new OkHttpClient();
url = Constants.moviebaseurl + mEdit.getText();

Request request = new Request.Builder()
.url(url)
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
}

@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
final String myResponse = response.body().toString();

SearchActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject json = new JSONObject(myResponse);
JSONArray results = json.getJSONArray("results");
mText.setText(results.getJSONObject(0).getString("title"));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}

我是做错了什么,还是只是遗漏了什么?感谢您的帮助!

最佳答案

它改变了

final String myResponse = response.body().toString();

对于

final String myResponse = response.body().string();

关于android - java.lang.String 类型的值在传递 http 响应时无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59111026/

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