インターネット上のチュートリアルの 1 つから非常に単純な例を実行しようとしています。しかし、実行できません。http getリクエストをサーバーに送信すると例外が発生します....サーバーにリクエストを送信するまで、コードは正常に実行されます。
HttpExample クラス:
public class HttpExample extends Activity {
TextView httpStuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httpex);
httpStuff = (TextView) findViewById(R.id.tvHttp);
GetMethodEx test = new GetMethodEx();
String returned;
try {
returned = test.getInternetData();
httpStuff.setText(returned);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(HttpExample.this, "Failed", Toast.LENGTH_LONG)
.show();
}
}
public class GetMethodEx {
public String getInternetData() throws Exception {
BufferedReader in = null;
String data = null;
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.mybringback.com/");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
// Toast.makeText(HttpExample.this, "Here",
// Toast.LENGTH_LONG).show();
in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally {
if (in != null) {
try {
in.close();
return data;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
Android Manifest.xml : インターネット権限も有効にしました
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.httpclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.httpclient.HttpExample"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
サーバーからデータを送受信するために非常に多くの他のコードを実行したため、何が問題なのかわかりませんが、それらも実行されないため、エミュレーターまたは何かの問題である可能性があります...これについて教えてくださいクラスコードとマニフェストは次のとおりです……