Android GET方式和POST方式提交给WEB服务器
来源:互联网 发布:程序员要出差吗 编辑:程序博客网 时间:2024/05/22 10:49
一、GET方式:
public void testRequestByGET(){try {StringBuffer sb = new StringBuffer();sb.append("http://192.168.241.1:8080/test/servlet/ManageServlet?title='");sb.append(URLEncoder.encode("中国", "UTF-8"));sb.append("'&time=10");URL url = new URL(sb.toString());HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setReadTimeout(5000);conn.setRequestMethod("GET");if(conn.getResponseCode() == 200){Log.i(TAG, "success");} else {Log.i(TAG, "fail");}} catch (Exception e) {e.printStackTrace();}}
二、POST方式:
public void save(){try {String path = new String("http://172.30.1.114:8080/test/servlet/ManageServlet");Map<String, String> params = new HashMap<String, String>();params.put("title", "少年时代");params.put("time", String.valueOf(10));// sendPOSTRequest(path, params, "UTF-8");<span style="white-space:pre"></span>sendHttpClientPOSTRequest(path, params, "UTF-8");<span style="white-space:pre"></span>} catch (Exception e) {e.printStackTrace();}}
</pre><pre code_snippet_id="538522" snippet_file_name="blog_20141201_2_5669117" name="code" class="java">private boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{StringBuilder sb = new StringBuilder();if(params != null && !params.isEmpty()){for(Map.Entry<String, String> entry : params.entrySet()){sb.append(entry.getKey()).append("=");sb.append(URLEncoder.encode(entry.getValue(), encoding));// 使用URLENcoder.encode进行编码sb.append("&");}sb.deleteCharAt(sb.length()-1);// 去掉最后一个“&”byte[] entry = sb.toString().getBytes();// 得到实体数据HttpURLConnection conn = (HttpURLConnection)(new URL(path)).openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("POST");conn.setDoOutput(true);// 允许往外输出数据流conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");conn.setRequestProperty("Content-Length", String.valueOf(entry.length));OutputStream outputStream = conn.getOutputStream();outputStream.write(entry);if(conn.getResponseCode() == 200){Log.i(TAG, "success");return true;}}Log.i(TAG, "fail");return false;}
/** * 第二种方法:使用HttpClient发送POST请求 * @return * @throws Exception */public boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{List<NameValuePair> pairs = new ArrayList<NameValuePair>();// 存放请求参数StringBuilder sb = new StringBuilder();if(params != null && !params.isEmpty()){for(Map.Entry<String, String> entry : params.entrySet()){pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}}UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding);HttpPost httpPost = new HttpPost(path);httpPost.setEntity(entity);DefaultHttpClient httpClient = new DefaultHttpClient();// DefaultHttpClient:看做浏览器HttpResponse response = httpClient.execute(httpPost);if(response.getStatusLine().getStatusCode() == 200){return true;}return false;}
0 0
- Android GET方式和POST方式提交给WEB服务器
- android 通过get,post方式提交数据给服务器
- Android通过Get,Post,HttpClient方式提交参数给服务器
- Android通过Get,Post,HttpClient方式提交参数给服务器
- 使用GET和POST方式将数据提交给服务器
- 通过GET和POST方式提交参数给web应用
- 通过get和post方式提交参数给web应用
- Android开发入门之通过GET和POST方式提交参数给web应用
- Android 网络通信之通过GET和POST方式提交参数给web应用
- Android 通过GET和POST方式提交参数给web应用
- android中用get和post方式向服务器提交请求
- android中get方式和post方式提交数据到服务器的区别
- Android向服务器提交数据(方式:get、post、AsyncHttpClient )
- c# POST和GET方式通过服务器地址提交数据
- Android 使用GET和PUT方式以及用HttpClient方式向web服务器提交参数
- Android学习笔记---27_网络通信之通过GET和POST方式提交参数给web应用,以及使用httpClient,来给web项目以post方式发送参数
- android之使用get和post方式向服务器提交请求
- android之使用get和post方式向服务器提交请求
- 10个程序员必须学会接受的残酷真相
- 一直没有人懂我,我习惯假装坚强
- java编码小细节注意
- iOS 开发者账号初试
- hadoop MapReduce实例详解
- Android GET方式和POST方式提交给WEB服务器
- 21还在为没有电工证,不能上岗工作发愁?华南泉州电工培训学校帮您解决
- 在eclipse中通过wifi调试android程序
- 查找运行速度慢的sql语句
- codeigniter里的登录跳转
- 推送
- POJ 1065 Wooden Sticks (贪心)
- 电老虎又发威了:凋谢“江南一枝花”
- Selenium2.0之WebDriver学习总结(一)