httpurlconnection post 和GET 分别传递2个参数给后台,返回JSON 解决乱码问题

本文介绍了如何使用HTTPURLConnection在Android中进行POST和GET请求,传递两个参数给后台,并解决了返回JSON数据时可能出现的乱码问题。在POST请求中,直接拼接参数并使用URLEncoder.encode进行编码,而在GET请求中,参数直接附加在URL后面。同时,服务端需设置正确的字符编码以避免乱码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



不需要封装对象参数, 直接和 get方式一样 拼接 param=1&pame=244 就可以传递给后台。后台使用servlet测试,显示 的确是 doPost在处理,而且2个参数都能收

也没有乱码出现

 

只需要给这个函数传递一个 URL  ,然后在里面定义参数


public static String  urljsonPOST(String fromurl)
 {
  //网络地址 通过字符串,生成URL对象
  URL url=null;
  // 网络会话链接
  HttpURLConnection conn=null;
     //获取网站返回的输入流
  InputStream in=null;
  //每次读的字节数
  byte[] data =new byte[1024];
  //每次读到的字节数,一般是1024,如果到了最后一行就会少于1024,到了末尾就是 -1
  int len=0; 
  //本地的输出流
  ByteArrayOutputStream os;
  try
  {
   Log.v("zms","使用httpurlconnection");
    url=new URL(fromurl);
    conn=(HttpURLConnection)url.openConnection();
     //默认是get 方式
     conn.setRequestMethod("POST");
      // 设置是否向connection输出,如果是post请求,参数要放在http正文内,因此需要设为true
          conn.setDoOutput(true);
    // Post 请求不能使用缓存
           conn.setUseCaches(false);
   //设置请求头 一般没特殊要求, 不需要
         //conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
          // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
          
         // 要注意的是connection.getOutputStream会隐含的进行connect,所以下面这句可以不要
          //conn.connect();
           //要上传的参数 
            PrintWriter pw=new PrintWriter(conn.getOutputStream());
            String content = "param1=" + URLEncoder.encode("日本7.7", "UTF_8")+"&param2="+ URLEncoder.encode("中国8.15", "UTF_8");  
            pw.print(content);
            pw.flush();
            pw.close();
   //时候需要获取输入, 废话,当然需要返回,最少要返回状态吧。 所以默认是true
    //conn.setDoInput(true);
    in=conn.getInputStream();
    os=new ByteArrayOutputStream(); 
   while ((len=in.read(data))!=-1)
   {
    os.write(data,0,len);
   }
   in.close();
   return new String(os.toByteArray());
  } catch (Exception e)
  {
   e.printStackTrace();
  }
 
  return null;
 }





服务端的情况:


 




---------------------------------------------------------------------get方法


服务端 要注意 编码


response.setContentType("text/html;charset=UTF-8");
  response.setHeader("Cache-Control", "no-cache");
  PrintWriter out = response.getWriter();
  request.setCharacterEncoding("utf-8");
  
   System.out.println("hello word  get");
  String  param1="";
  String  param2="";

  if ((request.getParameter("param1")!=null) & (request.getParameter("param2")!=null) )
  {             
   param1=new String(request.getParameter("param1").getBytes("iso-8859-1"),"GBK");




-----------------------------------------get android

public static String  urljsonGET(String fromurl)
 {
  //网络地址 通过字符串,生成URL对象
  URL url=null;
  // 网络会话链接
  HttpURLConnection conn=null;
     //获取网站返回的输入流
  InputStream in=null;
  //每次读的字节数
  byte[] data =new byte[1024];
  //每次读到的字节数,一般是1024,如果到了最后一行就会少于1024,到了末尾就是 -1
  int len=0; 
  //本地的输出流
  ByteArrayOutputStream os;
  try
  {
   
   Log.v("zms","使用httpurlconnectionget");
     //服务器端
  /* response.setContentType("text/html;charset=UTF-8");
   response.setHeader("Cache-Control", "no-cache");
   PrintWriter out = response.getWriter();
   request.setCharacterEncoding("utf-8");
   param1=new String(request.getParameter("param1").getBytes("iso-8859-1"),"GBK");
   */
   String content = "param1="+URLEncoder.encode("日本7.7", "GBK")+"&param2="+URLEncoder.encode("中华民国1945.8.15", "GBK");  
    url=new URL(fromurl+"?"+content);
    conn=(HttpURLConnection)url.openConnection();
  
    //conn.setDoInput(true);
    in=conn.getInputStream();
    os=new ByteArrayOutputStream(); 
   while ((len=in.read(data))!=-1)
   {
    os.write(data,0,len);
   }
   in.close();
   return new String(os.toByteArray());
  } catch (Exception e)
  {
   e.printStackTrace();
  }
 
  return null;
 }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值