1、Android与本地服务器(同一台电脑)通迅,必须为电脑添加一个IP: 10.0.0.2,否则无法连接上本地服务器。
2、服务器接收端
这里就不写了 直接在model里面设置要获到的属性名字就行,如要接收 title,就直接用this.model.getTitle();
3、客户端(发送端)
- public void httpPost2()
- {
- try
- {
- show.setText(" post2 "+new Date().toString());
- // http地址
- String httpUrl = "http://10.0.0.2/wap/doAddNews.action";
- //HttpPost连接对象
- HttpPost httpRequest = new HttpPost(httpUrl);
- //使用NameValuePair来保存要传递的Post参数
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- //添加要传递的参数
- params.add(new BasicNameValuePair("title", " 中文哦HttpClient_android_Post"));
- //设置字符集
- HttpEntity httpentity = new UrlEncodedFormEntity(params, "UTF-8");
- //请求httpRequest
- httpRequest.setEntity(httpentity);
- //取得默认的HttpClient
- HttpClient httpclient = new DefaultHttpClient();
- //取得HttpResponse
- HttpResponse httpResponse = httpclient.execute(httpRequest);
- //HttpStatus.SC_OK表示连接成功
- if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
- {
- //取得返回的字符串
- String strResult = EntityUtils.toString(httpResponse.getEntity());
- // show.setText(strResult);
- System.out.println("返回结果:/r/n"+strResult);
- }
- else
- {
- show.setText("请求错误!");
- }
- }catch(Exception ex)
- {
- System.out.println(ex.toString());
- }
- }
注意:客户端和服务端使用的都是 UTF-8, 按道理讲 用GB2312也行,
不过客户端的字符编码和服务器使用的字符编码必须一致,
否则,有可能出现乱码。