gpt4 book ai didi

java - 将API数据解析到TextView中

转载 作者:行者123 更新时间:2023-12-01 17:54:45 24 4
gpt4 key购买 nike

我有一个 API 链接,其中有一个名为 title 的 json 元素,我试图将该值存储到 TextView 中。到目前为止,我的主要 Activity 代码中应该显示包含的字符串:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONObject jObject;
try {
jObject = new JSONObject("https://chex-triplebyte.herokuapp.com/api/cats?page=0");
String mResponse = jObject.getString("title");
TextView t = (TextView) findViewById(R.id.title_image);
t.setText(mResponse);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我提供的 API 链接有效,因此您可以看到我尝试获取的值标题。

最佳答案

试试这个:

  TextView t = (TextView) findViewById(R.id.title_image);
try {

url = new URL("https://chex-triplebyte.herokuapp.com/api/cats?page=0");

urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.connect();

InputStream in = urlConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
//InputStreamReader isw = new InputStreamReader(in);
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}

String JsonResponse= buffer.toString();

JSONObject jsonobj = new JSONObject(JsonResponse);
JSONArray jarray = jsono.getJSONArray("jsontitle");

for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);


t.setText(object.getString("title"));


}
} catch (JSONException e) {
e.printStackTrace();
}

但你必须确保你有正确的 json 格式,如下所示:

{jsontitle:[{"title":"Space Keybaord Cat","timestamp":"2017-09-11T04:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/space.jpg","description":"In space, no one can hear you purr."},{"title":"Jiji","timestamp":"2017-09-11T03:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/jiji.png","description":"You'd think they'd never seen a girl and a cat on a broom before"},{"title":"Limecat","timestamp":"2017-09-11T02:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/lime.jpg","description":"Destroyer of Clockspider and his evil followers, Limecat is the one true god."},{"title":"Astronaut Cat","timestamp":"2017-09-11T01:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/astronaut.jpg","description":"Houston, we have a purroblem"},{"title":"Grumpy Cat","timestamp":"2017-09-11T00:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/grumpy.jpg","description":"Queen of the RBF"},{"title":"Soviet cat","timestamp":"2017-09-10T23:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/soviet.jpg","description":"In soviet Russia cat pets you!"},{"title":"Serious Business Cat","timestamp":"2017-09-10T22:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/serious.jpg","description":"SRSLY GUISE"},{"title":"Sophisticated Cat","timestamp":"2017-09-10T21:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/sophisticated.PNG","description":"I should buy a boat"},{"title":"Shironeko","timestamp":"2017-09-10T20:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/shironeko.png","description":"The zen master kitty"},{"title":"Puss in Boots","timestamp":"2017-09-10T19:00:04Z","image_url":"https://triplebyte-cats.s3.amazonaws.com/puss.jpg","description":"Don't you dare do the litter box on me!"}]}

关于java - 将API数据解析到TextView中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46147820/

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