Reputation: 37
I am a newbie in android and java. I want to get a url request (result is JSON) and parsing it (for example get JSON weather from yahoo api's). I copy getStringFromUrl Function and I know error for my function (setWeather). please help me.
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);
}
When I run this app on device app crash. It's error that write on Android Monitor:
Upvotes: 0
Views: 145
Reputation: 680
All the network requests should be made on a separate worker thread or else you will get NetworkOnMainThread exception. For your use case use an Asynctask which has method doInBackground() to handle your request on background thread and post results back to main Ui thread inside onPostExecute() method. So call below method in in doInBackground() method.
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");
and use ui components like textview in onPostExecute() method
tv.setText(last);
which runs on ui thread. All this management is done by Asnyctask so you don't need to worry about thread management just know which method to use. Asynctask Android documentation
Upvotes: 1
Reputation: 773
In case of android there is one notion you have to follow, all time taking tasks need to go on a separate thread that is not blocking your UI thread. And all IO calls or heavy operation call should go onto a seperate thread.
For more about how to make network operations refer Android developer guide here (https://developer.android.com/training/basics/network-ops/connecting.html) and follow this document.
Upvotes: 1