私は Android 用のアプリを開発しており、URL から Web ページのコンテンツを取得する方法のすべてではないにしても、ほとんどの方法を試しました。
私が使用したコードは次のとおりです。
public void login(View view) throws ClientProtocolException, IOException {
Logger logger = Logger.getLogger("schedulous");
logger.info("BUTTON CLICKED");
HttpGet request = new HttpGet("http://www.google.com/");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String entityContents = EntityUtils.toString(entity);
logger.info(entityContents);
}
そして、私が得るエラーは次のとおりです。
09-09 17:47:02.021: E/AndroidRuntime(1611): FATAL EXCEPTION: main
09-09 17:47:02.021: E/AndroidRuntime(1611): java.lang.IllegalStateException: Could not execute method of the activity
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.view.View$1.onClick(View.java:3591)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.view.View.performClick(View.java:4084)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.view.View$PerformClick.run(View.java:16966)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.os.Handler.handleCallback(Handler.java:615)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.os.Looper.loop(Looper.java:137)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 17:47:02.021: E/AndroidRuntime(1611): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-09 17:47:02.021: E/AndroidRuntime(1611): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-09 17:47:02.021: E/AndroidRuntime(1611): at dalvik.system.NativeStart.main(Native Method)
09-09 17:47:02.021: E/AndroidRuntime(1611): Caused by: java.lang.reflect.InvocationTargetException
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.view.View$1.onClick(View.java:3586)
09-09 17:47:02.021: E/AndroidRuntime(1611): ... 11 more
09-09 17:47:02.021: E/AndroidRuntime(1611): Caused by: android.os.NetworkOnMainThreadException
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-09 17:47:02.021: E/AndroidRuntime(1611): at com.schedulous.me.Main.login(Main.java:41)
09-09 17:47:02.021: E/AndroidRuntime(1611): ... 14 more
また、マニフェストに適切な権限が設定されていることも保証します。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>