我如何解析 URL 请求的 JSON 结果?
How I can parse JSON result from URL request?
我是 android 和 java 的新手。我想得到一个 url 请求(结果是 JSON)并解析它(例如从雅虎 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监视器上出错:
在 android 的情况下,您必须遵循一个概念,所有时间任务都需要在一个单独的线程上进行,该线程不会阻塞您的 UI 线程。所有 IO 调用或繁重的操作调用都应进入单独的线程。
有关如何进行网络操作的更多信息,请参阅 Android 开发人员指南
在此处 (https://developer.android.com/training/basics/network-ops/connecting.html) 并遵循此文档。
所有的网络请求都应该在一个单独的工作线程上进行,否则你会得到 NetworkOnMainThread 异常。对于您的用例,使用具有方法 doInBackground() 的 Asynctask 来处理您对后台线程的请求,并且 post 结果返回到 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
我是 android 和 java 的新手。我想得到一个 url 请求(结果是 JSON)并解析它(例如从雅虎 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监视器上出错:
在 android 的情况下,您必须遵循一个概念,所有时间任务都需要在一个单独的线程上进行,该线程不会阻塞您的 UI 线程。所有 IO 调用或繁重的操作调用都应进入单独的线程。
有关如何进行网络操作的更多信息,请参阅 Android 开发人员指南 在此处 (https://developer.android.com/training/basics/network-ops/connecting.html) 并遵循此文档。
所有的网络请求都应该在一个单独的工作线程上进行,否则你会得到 NetworkOnMainThread 异常。对于您的用例,使用具有方法 doInBackground() 的 Asynctask 来处理您对后台线程的请求,并且 post 结果返回到 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