Android的提交数据到服务器

来源:互联网 发布:ws源码 编辑:程序博客网 时间:2024/05/22 05:07

在Android中提交数据到服务器和平时的浏览器差不多分为三步:

第一步:打开一个浏览器,

第二部:输入一个网址;

第三部:敲回车。

Get实现代码例:

//打开一个浏览器HttpClient client = new DefaultHttpClient();String path = "http://192.168.1.100:8080/web/LoginServlet?username=" + username + "&password=" + password;//输入网址HttpGet httpGet = new HttpGet(path);//敲回车HttpResponse response = client.execute(httpGet);int code = response.getStatusLline().getStatusCode();if(code == 200){    InputStream is = response.getEntity.getContent();    String text = StreamTools.readInputStream(is);}

Post实现代码例:

//打开一个浏览器HttpClient client = new DefaultHtttClient();//输入网址String path = "http://192.168.1.100:8080/web/LoginServlet";HttpPost httpPost = new HttpPost(path);//指定要提交的数据实体List<NameValuePair> parameters = new ArrayList<NameValuePair>();parameters.add(new BasicNameValuePair("username", username));parameters.add(new BasicNameValuePair("password", password));httpPost.setEntity(new UrlEncodeFormEntiry(parameters, "UTF-8"));//敲回车HttpResponse response = client.execute(httpPost);int code = response.getStatusLine().getStatusCode();










0 0
原创粉丝点击