Android的JSON解析器URL(NON DEPECREATED) [英] Android JSON Parser From URL (NON DEPECREATED)

查看:202
本文介绍了Android的JSON解析器URL(NON DEPECREATED)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我来接近我束手无策这儿。我一直在下面code的例子和阅读如何打一个网址,并在JSON对象以下的Web请求读取线程。但是,这些样品似乎都使用的HttpClient和Htt的presponse的Andr​​oid工作室说是去precated。 Android的API说,使用URL连接,但我似乎无法理解如何使用URLConnection的格局。

Okay I'm coming close to my wits end here. I have been following code samples and reading threads on how to hit a URL and read in the JSON object following that web request. But these samples all seem to use HttpClient and HttpResponse which android studio is saying is deprecated. Android API says to use URL Connection, but I can't seem to understand how to use the pattern with URLConnection.

那么,如何使用的URLConnection打一个URL,然后读取JSON对象。后来我可以反序列化自己作为JSONObject的不是去precated。我奋力如何启动Web请求。

So how do I use URLConnection to hit a URL, and then read the JSON objects. I can deserialize myself later as JSONObject is NOT deprecated. I am struggling on how to initiate the web request.

任何人有code段?样品?在线提供的资料?

Anyone have code snippets? Samples? Material available online?

推荐答案

从的此示例项目由 rel=\"nofollow\">这本书,这里是加载最新的堆栈溢出的 使用 HttpURLConnection类安卓问题,并利用它们进行解析GSON:

From questions using HttpURLConnection and parses them using Gson:

/***
  Copyright (c) 2013-2014 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.hurl;

import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.Gson;
import de.greenrobot.event.EventBus;

class LoadThread extends Thread {
  static final String SO_URL=
      "https://api.stackexchange.com/2.1/questions?"
          + "order=desc&sort=creation&site=stackoverflow&tagged=android";

  @Override
  public void run() {
    try {
      HttpURLConnection c=
          (HttpURLConnection)new URL(SO_URL).openConnection();

      try {
        InputStream in=c.getInputStream();
        BufferedReader reader=
            new BufferedReader(new InputStreamReader(in));
        SOQuestions questions=
            new Gson().fromJson(reader, SOQuestions.class);

        reader.close();

        EventBus.getDefault().post(new QuestionsLoadedEvent(questions));
      }
      catch (IOException e) {
        Log.e(getClass().getSimpleName(), "Exception parsing JSON", e);
      }
      finally {
        c.disconnect();
      }
    }
    catch (Exception e) {
      Log.e(getClass().getSimpleName(), "Exception parsing JSON", e);
    }
  }
}

在这里, SOQuestions 与堆栈交换问题的API GSON注解类,我使用greenrobot的EventBus到一个片段得到的结果。当然,你会用自己更换堆叠交换API URL和需要解析不论其JSON输出。

Here, SOQuestions is the Gson-annotated class for the Stack Exchange questions API, and I am using greenrobot's EventBus to get the results over to a fragment. Of course, you would replace the Stack Exchange API URL with your own and would need to parse whatever its JSON output is.

我不知道的很多使用Android开发的JSONObject 了,鉴于GSON,杰克逊等很多更好的选择,但你需要阅读的InputStream 字符串传递给适当的的JSONObject 构造函数。

I'm not aware of many Android developers using JSONObject anymore, given much better options in Gson, Jackson, etc., but you would need to read in the InputStream into a String to pass to the appropriate JSONObject constructor.

您也可以考虑使用更高阶的方法。例如,这个示例应用程序是第一个说的克隆使用方的改造库联系堆栈交易所API的Web服务,而不是 HttpURLConnection类

You might also consider using higher-order approaches. For example, this sample app is a clone of the first that uses Square's Retrofit library for contacting the Stack Exchange API Web service, rather than HttpUrlConnection.

这篇关于Android的JSON解析器URL(NON DEPECREATED)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆