[英]learning android..getting IO exception in httpURL
我正在学习android,并且陷入IO异常yy块中。 有人能帮我吗。 下面是我的代码
package com.example.blogreader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainListActivity extends ListActivity {
protected String[] mHospitals;
public static final int NUMBER_OF_HOSPITALS = 20;
public static final String TAG = MainListActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
//
// Resources resource = getResources();
// mHospitals = resource.getStringArray(R.array.hospital_names);
//
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mHospitals);
// setListAdapter(adapter);
//Toast.makeText(this, getString(R.string.no_hospitals_fond),Toast.LENGTH_LONG).show();
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_list, menu);
return true;
}
private class GetBlogPostsTask extends AsyncTask<Object, Void, String> {
protected String doInBackground(Object... arg0) {
int reponseCode = -1;
try {
URL hospitalFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_HOSPITALS);
HttpURLConnection connection = (HttpURLConnection) hospitalFeedUrl.openConnection();
connection.connect();
reponseCode = connection.getResponseCode();
Log.i(TAG, "Code " + reponseCode);
}
catch (MalformedURLException e) {
Log.e(TAG, "exception caught xx :", e);
}
catch (IOException e) {
Log.e(TAG, "exception IO caught yy : ", e);
}
catch(Exception e){
Log.e(TAG, "General exception zz :", e);
}
return "Code "+ reponseCode;
}
}
}
如果没有logcat,这是一个很疯狂的猜测,但是您可能忘记了将INTERNET权限授予您的AndroidManifest.xml
吗?
<uses-permission android:name="android.permission.INTERNET" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.