简体   繁体   中英

Android Unable to start activity ComponentInfo - JSON response

I have this code that should be parsing a JSON response from a flickr account. When I paste the url into a browser, I get a JSON response. So the URL is good. Any help would be greatly appreciated!

Here is my code:

From DownloadHelper.java

public String executeGet(Context context, String urlString, 
        boolean authenticated) throws IOException {
    Log.v(this.getClass().getSimpleName(), "Requesting URL: " + urlString);
    URL url = new URL(urlString);
    Log.d(this.getClass().getSimpleName(),"before urlConnection");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    Log.d(this.getClass().getSimpleName(),"Before setRequest");
    urlConnection.setRequestProperty("User-Agent", buildUserAgent(context));

    //todo
    //     if (authenticated && mAuthToken != null) {
//        urlConnection.setRequestProperty("Authorization", "Bearer " + mAuthToken);
//    }
    Log.d(this.getClass().getSimpleName(),"Before connect");
    urlConnection.connect();
    Log.d(this.getClass().getSimpleName(),"Connected");
    throwErrors(urlConnection);
    Log.d(this.getClass().getSimpleName(),"Reading Input");
    String response = readInputStream(urlConnection.getInputStream());
    Log.v(this.getClass().getSimpleName(), "HTTP response: " + response);
    return response;
}

Called from FlickrActivity.java:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DownloadHelper client = new DownloadHelper();
    Log.d(TAG, "Starting FlickrActivity" );
    flickrAPI = getString(R.string.flickr_api_key);
    flickrUID = getString(R.string.flickr_user_id);
    flickrPerPage = "12";
    flickrURL = "http://api.flickr.com/services/rest/?&method=flickr.photosets.getList&api_key="+flickrAPI+"&user_id="+flickrUID+"&format=json";
    //Log.d(TAG, "TPS Starting Request"+client.getResponseObject(FlickrResponse.class, flickrURL, false));
//      client.getResponseObject(null, flickrURL, false);
    try {
        response = client.executeGet(getBaseContext(), flickrURL, false);
        Log.d(TAG, "response = "+response);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d(TAG,"Response didn't work");
    }

}
}

You should avoid making Internet calls on the UIThread . Instead do them in an AsyncTask or Thread . Try to move your code from onCreate to doInbackground of an AsyncTask

In onCreate you call executeGet . In executeGet you have the below

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// should be executed on a thread

You are running Network related operation on the ui thread. Post HoneyComb you get NetworkOnMainthreadException

You need to use a thread or Asynctask

Check the docs for asynctask

http://developer.android.com/reference/android/os/AsyncTask.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM