我对 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
}
任何人都可以找出原因吗?