Im just a beginner learning Json parsing and stream from the net, well I dont get errors in this app but it doesnt display anything. I dont know what the problem and cant see any problem in the log. here is the code:
InputStream is;
String line;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text);
try {
URL url = new URL("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&minlatitude=4&maxlatitude=5");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
is = new BufferedInputStream(connection.getInputStream());
if(connection.getInputStream()==null)
{
textView.setText("input stream empty");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
while((line=reader.readLine())!=null){
builder.append(line);
}
if(builder.toString().equals(""))
{
textView.setText("no work builder empty");
}
line=builder.toString();
JSONObject object = new JSONObject(line);
JSONArray fea = object.getJSONArray("features");
JSONObject QUAKE = fea.getJSONObject(0);
JSONObject pro = QUAKE.getJSONObject("properties");
int mag = pro.getInt("mag");
textView.setText(mag+"");
} catch (Exception e) {
e.printStackTrace();
}
}
Thanks!