20171024
http://weather.livedoor.com/forecast/webservice/json/v1?city=300020
https://codezine.jp/article/detail/9746
///
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
///
package jp.co.wata.myapplication;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WeatherInfoReceiver weatherInfoReceiver = new WeatherInfoReceiver();
weatherInfoReceiver.execute("300020");
}
}
class WeatherInfoReceiver extends AsyncTask<String, String, String> {
public String doInBackground(String... params) {
String id = params[0];
String urlStr = "http://weather.livedoor.com/forecast/webservice/json/v1?city=" + id;
HttpURLConnection con = null;
InputStream is = null;
String result = "";
try {
URL url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
is = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sb = new StringBuffer();
char[] b = new char[1024];
int line;
while(0 <= (line = reader.read(b))) {
sb.append(b, 0, line);
}
result= sb.toString();
}
catch(MalformedURLException ex) {
}
catch(IOException ex) {
}
finally {
if(con != null) {
con.disconnect();
}
if(is != null) {
try {
is.close();
}
catch(IOException ex) {
}
}
}
return result;
}
public void onPostExecute(String result) {
try {
JSONObject rootJSON = new JSONObject(result);
//System.out.println(rootJSON.getString("pinpointLocations"));
JSONObject descriptionJSON = rootJSON.getJSONObject("description");
//System.out.println(rootJSON.getJSONObject("description"));
//System.out.println(descriptionJSON.getString("text"));
JSONArray forecasts = rootJSON.getJSONArray("forecasts");
JSONObject forecastNow = forecasts.getJSONObject(0);
//System.out.println( forecastNow.getString("dateLabel"));
System.out.println( forecastNow.getString("telop"));
}
catch(JSONException ex) {
}
}
}