java - 将API数据解析到TextView中

标签 java android textview

我有一个 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/

相关文章:

java - hibernate :标识符

java - 如何解决,JSONException : No value for "1"

android - 如何从 WebView 引用存储在内部存储中的文件?

android - 使 ExpandableListView 和 Textview 一起滚动

android - 解决textview转位图时黑屏

java - 错误 : A universal match pattern ('/**' ) is defined before other patterns in the filter chain

java - 更改生成的 jaxb 类的包

java - 如何创建自定义 Quartz 触发器?

java - 如何将数字与值的字母指针转换。示例 : 13486 to 13. 4K

android - TextView - 在另一个 TextView 的右侧开始一个新行