私はちょうどウェブサイトからデータを取得することを学ぼうとしています。まず、ステータス コードを取得して出力します。
public class Json extends Activity{
TextView tvStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.json);
tvStatus = (TextView) findViewById(R.id.tvStatus);
try {
String data = readLocalhost();
tvStatus.setText(data);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String readLocalhost() throws ClientProtocolException, IOException
{
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://localhost");
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
String data = status.toString();
return data;
}
私はすでにインターネット許可を追加しました。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.channel"
android:versionCode="1"
android:versionName="1.0"
android:name="android.permission.INTERNET"
>
そして、これがlogcatです。
10-22 15:30:42.292: I/Choreographer(1011): Skipped 33 frames! The application may be doing too much work on its main thread.
10-22 15:30:43.032: D/AndroidRuntime(1011): Shutting down VM
10-22 15:30:43.032: W/dalvikvm(1011): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-22 15:30:43.182: D/dalvikvm(1011): GC_FOR_ALLOC freed 147K, 9% free 2867K/3132K, paused 53ms, total 58ms
10-22 15:30:43.182: E/AndroidRuntime(1011): FATAL EXCEPTION: main
10-22 15:30:43.182: E/AndroidRuntime(1011): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.channel/com.example.channel.Json}: android.os.NetworkOnMainThreadException
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.os.Looper.loop(Looper.java:137)
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-22 15:30:43.182: E/AndroidRuntime(1011): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 15:30:43.182: E/AndroidRuntime(1011): at java.lang.reflect.Method.invoke(Method.java:525)
10-22 15:30:43.182: E/AndroidRuntime(1011): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-22 15:30:43.182: E/AndroidRuntime(1011): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 15:30:43.182: E/AndroidRuntime(1011): at dalvik.system.NativeStart.main(Native Method)
10-22 15:30:43.182: E/AndroidRuntime(1011): Caused by: android.os.NetworkOnMainThreadException
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
10-22 15:30:43.182: E/AndroidRuntime(1011): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-22 15:30:43.182: E/AndroidRuntime(1011): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-22 15:30:43.182: E/AndroidRuntime(1011): at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-22 15:30:43.182: E/AndroidRuntime(1011): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-22 15:30:43.182: E/AndroidRuntime(1011): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-22 15:30:43.182: E/AndroidRuntime(1011): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-22 15:30:43.182: E/AndroidRuntime(1011): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-22 15:30:43.182: E/AndroidRuntime(1011): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-22 15:30:43.182: E/AndroidRuntime(1011): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-22 15:30:43.182: E/AndroidRuntime(1011): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-22 15:30:43.182: E/AndroidRuntime(1011): at com.example.channel.Json.readLocalhost(Json.java:43)
10-22 15:30:43.182: E/AndroidRuntime(1011): at com.example.channel.Json.onCreate(Json.java:27)
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.app.Activity.performCreate(Activity.java:5133)
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-22 15:30:43.182: E/AndroidRuntime(1011): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-22 15:30:43.182: E/AndroidRuntime(1011): ... 11 more
10-22 15:32:24.912: I/Process(1011): Sending signal. PID: 1011 SIG: 9
この問題を解決するには?私は初心者だと言いました。回答はできるだけ明確に入力してください。ありがとう。