public class MainActivity extends Activity {
/** Called when the activity is first created. */
//模拟器自己把自己当成localhost了,服务器应该为10.0.2.2
private static String url="http://10.0.2.2:8080/PDAServer/login.action";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getPDAServerData(url);
}
/**
* 请求服务
* @param url
*/
private void getPDAServerData(String url){
url+="?username=123456";
HttpClient client=new DefaultHttpClient();
HttpPost request;
try {
request = new HttpPost(new URI(url));
HttpResponse response=client.execute(request);
//判断请求是否成功
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity=response.getEntity();
if(entity!=null){
String out=EntityUtils.toString(entity);
new AlertDialog.Builder(this).setMessage(out).create().show();
}
}
}catch (URISyntaxException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果如下:
成功了吧:当然读者可以根据这个思路扩展下应用,比如:struts2+hibernate+spring+json。数据方面可以用json,也可以使用xml。android已经提供了JSON的API,struts2中也可以使用JSON插件.自己扩展吧。