1 Replies - 527 Views - Last Post: 29 March 2015 - 11:24 PM

#1 Riskinit   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 174
  • Joined: 14-January 10

Retrieving JSON from Weather API causes APP to immediately crash.

Posted 29 March 2015 - 03:05 PM

My experience with Android is pretty low but I've set up a class and added 2 methods and added a single button on my layout. I have also added permissions to the Manifest file.

package com.example.exjson;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity {

	protected void onCreate(Bundle savedInstanceState) {

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;

	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		return super.onOptionsItemSelected(item);
	public void searchClicked(View v){
		String weatherData;
		String requestUri = "http://api.wunderground.com/api/a8865fd6351f5b91/forecast/q/OH/Cincinnati.json";
		try {
			weatherData = request(requestUri);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		//JSONObject jsonObject = new JSONObject(rawPlantData);
	 * Invoke a URI (URL), and pass back the String result we get when we invoke that URI.
	 * @param uri the address (typically starting with http) that we want to call.
	 * @return the data we get when we invoke this address.
	 * @throws IOException 
	 * @throws ClientProtocolException 
	public String request(String uri) throws ClientProtocolException, IOException {
		// HttpGet represents the request that we are making
		HttpGet httpGet = new HttpGet(uri);
		// ResponseHandler handles the response from this request.
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		// HttpClient glues together the request and the response.
		HttpClient httpClient = new DefaultHttpClient();
		// make the request and response happen!
		String response = httpClient.execute(httpGet, responseHandler);
		// return the response.
		return response;

And the Manifest file is

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        android:targetSdkVersion="21" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


But when I run this Application in the Android Virtual Device the App immediately crashes. I am not sure if I am missing something obvious but has anyone done something similar? The goal here is to get JSON from a Weather API and return that as a string.

Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving JSON from Weather API causes APP to immediately crash.

#2 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Retrieving JSON from Weather API causes APP to immediately crash.

Posted 29 March 2015 - 11:24 PM

Android doesn't allows network tasks on the main thread. Use an AsyncTask to fetch the weather data from the API.

Note: Please do provide error log in future to get help more easily.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1