【问题标题】:How I can parse JSON result from URL request?如何解析来自 URL 请求的 JSON 结果?
【发布时间】:2017-08-17 19:47:23
【问题描述】:

我是 android 和 java 的新手。我想获取一个 url 请求(结果是 JSON)并解析它(例如从 yahoo api 获取 JSON 天气)。 我复制 getStringFromUrl 函数,我知道我的函数 (setWeather) 有错误。请帮帮我。

public static String getStringFromURL(String urlString) throws IOException {
    HttpURLConnection urlConnection;
    URL url = new URL(urlString);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setReadTimeout(10000 /* milliseconds */);
    urlConnection.setConnectTimeout(15000 /* milliseconds */);
    urlConnection.setDoOutput(true);
    urlConnection.connect();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
    char[] buffer = new char[1024];
    String outputString;
    StringBuilder builder = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        builder.append(line).append("\n");
    }
    bufferedReader.close();
    outputString = builder.toString();
    return outputString;
}

public void setWeather (View view) throws IOException, JSONException {
    String json = getStringFromURL("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='Esfahan')&format=json");
    JSONObject jso = new JSONObject(json);
    JSONObject query = jso.getJSONObject("query");
    JSONObject result = query.getJSONObject("results");
    JSONObject channel = result.getJSONObject("channel");
    JSONObject windI = channel.getJSONObject("wind");
    JSONObject location = channel.getJSONObject("location");
    String last = "";
    last = location.getString("city");
    TextView tv = (TextView) findViewById(R.id.textView);
    tv.setText(last);
}

当我在设备应用程序崩溃时运行此应用程序。 在 Android Monitor 上写是错误的:

【问题讨论】:

  • 错误是:codeat com.android.detf.testhttprequest.MainActivity.getStringFromURL(MainActivity.java:35) at com.android.detf.testhttprequest.MainActivity.setWeather(MainActivity.java: 50)code
  • 您需要在单独的线程上发出所有网络请求,然后您可以使用 UI 线程来更新 textview。
  • 我说我是新手;请用代码解释我或更多解释。
  • @Amir MainActivity.java 第 50 行是哪一行?

标签: android json httpurlconnection


【解决方案1】:

所有的网络请求都应该在一个单独的工作线程上进行,否则你会得到 NetworkOnMainThread 异常。对于您的用例,使用具有方法 doInBackground() 的 Asynctask 来处理您在后台线程上的请求并将结果发布回 onPostExecute() 方法内的主 Ui 线程。所以在 doInBackground() 方法中调用下面的方法。

getStringFromURL("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='Esfahan')&format=json");

并在 onPostExecute() 方法中使用 ui 组件,如 textview

tv.setText(last);

在 ui 线程上运行。所有这些管理都由 Asnyctask 完成,因此您无需担心线程管理,只需知道使用哪种方法即可。 Asynctask Android documentation

【讨论】:

    【解决方案2】:

    在 android 的情况下,您必须遵循一个概念,所有耗时的任务都需要在不会阻塞您的 UI 线程的单独线程上进行。并且所有的 IO 调用或繁重的操作调用都应该进入一个单独的线程。

    有关如何进行网络操作的更多信息,请参阅 Android 开发者指南 在这里 (https://developer.android.com/training/basics/network-ops/connecting.html) 并关注此文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多