私はAndroidプログラミングに非常に慣れていないので、HttpClientを使用してAndroidからphp Webサービスを呼び出そうとしました。しかし、アプリの実行中に「残念ながらServiceTestが停止しました」と言って停止し、終了しました。以下は私のコードです。
public class ServiceTestActivity extends Activity {
private Button btn;
private TextView txt;
String result = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
txt = (TextView) findViewById(R.id.txt_name);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callWebErvice("http://localhost/mvc/login/test");
}
});
}
public void callWebErvice(String serviceURL) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(serviceURL);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
txt.setText(result);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
Log.i("my result", result);
} // end callWebServe
}
誰でもこの理由を見つけることができますか?