设为首页 加入收藏

TOP

Android中POST方式发送HTTP请求(二)
2015-08-31 21:23:56 来源: 作者: 【 】 浏览:102
Tags:Android POST 方式 发送 HTTP 请求
e);//表示从服务器获取数据?


? ? ? ? ? ? ? connection.setDoOutput(true);//表示向服务器写数据?


? ? ? ? ? ? ? //获得上传信息的字节大小以及长度?


? ? ? ? ? ? ? ?


? ? ? ? ? ? ? connection.setRequestMethod("POST");?


? ? ? ? ? ? ? //是否使用缓存?


? ? ? ? ? ? ? connection.setUseCaches(false);?


? ? ? ? ? ? ? //表示设置请求体的类型是文本类型?


? ? ? ? ? ? ? connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");?


? ? ? ? ? ? ? ?


? ? ? ? ? ? ? connection.setRequestProperty("Content-Length", String.valueOf(mydata.length));?


? ? ? ? ? ? ? connection.connect();? //连接,不写也可以。。??有待了解?


? ? ? ? ? ? ?


? ? ? ? ? ? ? //获得输出流,向服务器输出数据?


? ? ? ? ? ? ? OutputStream outputStream = connection.getOutputStream();?


? ? ? ? ? ? ? ? ? outputStream.write(mydata,0,mydata.length);?


? ? ? ? ? ? ? ? ? //获得服务器响应的结果和状态码?


? ? ? ? ? ? ? int responseCode = connection.getResponseCode();?


? ? ? ? ? ? ? if(responseCode == HttpURLConnection.HTTP_OK){?


? ? ? ? ? ? ? ? return changeInputeStream(connection.getInputStream(),encode);?


? ? ? ? ? ? ? ? ? ?


? ? ? ? ? ? ? }?


? ? ? ? } catch (UnsupportedEncodingException e) {?


? ? ? ? ? ? // TODO Auto-generated catch block?


? ? ? ? ? ? e.printStackTrace();?


? ? ? ? } catch (IOException e) {?


? ? ? ? ? ? // TODO Auto-generated catch block?


? ? ? ? ? ? e.printStackTrace();?


? ? ? ? }?


? ? ? ? ?


? ? ? ? return "";?


? ? }?


? /**


? ? * 将一个输入流转换成字符串


? ? * @param inputStream


? ? * @param encode


? ? * @return


? ? */


? ? private static String changeInputeStream(InputStream inputStream,String encode) {?


? ? ? ? //通常叫做内存流,写在内存中的?


? ? ? ? ByteArrayOutputStream outputStream = new ByteArrayOutputStream();?


? ? ? ? byte[] data = new byte[1024];?


? ? ? ? int len = 0;?


? ? ? ? String result = "";?


? ? ? ? if(inputStream != null){?


? ? ? ? ? ? try {?


? ? ? ? ? ? ? ? while((len = inputStream.read(data))!=-1){?


? ? ? ? ? ? ? ? ? ? data.toString();?


? ? ? ? ? ? ? ? ? ? ?


? ? ? ? ? ? ? ? ? ? outputStream.write(data, 0, len);?


? ? ? ? ? ? ? ? }?


? ? ? ? ? ? ? ? //result是在服务器端设置的doPost函数中的?


? ? ? ? ? ? ? ? result = new String(outputStream.toByteArray(),encode);?


? ? ? ? ? ? ? ? outputStream.flush();?


? ? ? ? ? ? } catch (IOException e) {?


? ? ? ? ? ? ? ? // TODO Auto-generated catch block?


? ? ? ? ? ? ? ? e.printStackTrace();?


? ? ? ? ? ? }?


? ? ? ? }?


? ? ? ? return result;?


? ? }?


? ? public static void main(String[] arsg){?


? ? ? ? Map params = new HashMap();?


? ? ? ? params.put("username", "lili");?


? ? ? ? params.put("password", "123");?


? ? ? ? String result = sendPostMessage(params,"utf-8");?


? ? ? ? System.out.println("result->"+result);?


? ? }?


}?


下边是服务端的Servlet代码:


package com.login.manager;?


import java.io.ByteArrayOutputStream;?


import java.io.IOException;?


import java.io.InputStream;?


import java.io.InputStreamReader;?


import java.io.PrintWriter;?


import javax.servlet.ServletException;?


import javax.servlet.http.HttpServlet;?


import javax.servlet.http.HttpServletRequest;?


import javax.servlet.http.HttpServletResponse;?


public class LoginAction extends HttpServlet {?


? ? /**


? ? * Constructor of the object.


? ? */


? ? public LoginAction() {?


? ? ? ? super();?


? ? }?


? ? /**


? ? * Destruction of the servlet.


? ? */


? ? public void destroy() {?


? ? ? ? super.destroy(); // Just puts "destroy" string in log?


? ? ? ? // Put your code here?


? ? }?


? ? public void doGet(HttpServletRequest request, HttpServletResponse response)?


? ? ? ? ? ? throws ServletException, IOException {?


? ? ? ? this.doPost(request, response);?


? ? ?


? ? }?


? ? /**


? ? * The doPost method of the servlet.


? ? *


? ? * This method is called when a form has its tag value method equals to post.


? ? *?


? ? * @param request the request send by the client to the server


? ? * @param response the response send by the server to the client


? ? * @

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇利用Phoenix代理进行P2P WebRTC开.. 下一篇C#/iOS/Android通用加密解密方法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: