[英]Android Unable to start activity ComponentInfo - JSON response
I have this code that should be parsing a JSON response from a flickr account. 我有这段代码应该从flickr帐户解析JSON响应。 When I paste the url into a browser, I get a JSON response. 将网址粘贴到浏览器中时,我会收到JSON响应。 So the URL is good. 因此,URL是好的。 Any help would be greatly appreciated! 任何帮助将不胜感激!
Here is my code: 这是我的代码:
From DownloadHelper.java 从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: 从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
. 您应该避免在UIThread
上拨打互联网电话。 Instead do them in an AsyncTask
or Thread
. 而是在AsyncTask
或Thread
执行它们。 Try to move your code from onCreate
to doInbackground
of an AsyncTask
尝试将代码从onCreate
移到AsyncTask
doInbackground
In onCreate
you call executeGet
. 在onCreate
您可以调用executeGet
。 In executeGet
you have the below 在executeGet
您具有以下内容
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// should be executed on a thread
You are running Network related operation on the ui thread. 您正在ui线程上运行与网络相关的操作。 Post HoneyComb you get NetworkOnMainthreadException
发布HoneyComb后,您会收到NetworkOnMainthreadException
You need to use a thread
or Asynctask
您需要使用thread
或Asynctask
Check the docs for asynctask 检查文档中的asynctask
http://developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/android/os/AsyncTask.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.