android POST请求

作者: 我是涛锅锅 | 来源:发表于2017-09-08 16:04 被阅读26次

    Android用POST方式请求网络,多的不说了 直接上代码

    public static String Post(String string,String get,Context context)//string POST参数,get 请求的URL地址,context 联系上下文  
        {  
              
            try {  
            String urldizhi=get; //请求地址  
            URL url=new URL(urldizhi);  
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            conn.setConnectTimeout(50000);//超时时间  
            conn.setRequestMethod("POST");  
            conn.setDoOutput(true);  
            conn.setRequestProperty("Content-Type", "application/json");  
    //      conn.setRequestProperty("User-Agent", Other.getUserAgent(context));  
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());  
            out.write(string);  
            out.flush();  
            out.close();  
              
            InputStream inputStream=conn.getInputStream();  
            byte[] data=StreamTool.read(inputStream);  
            html = new String(data, "utf-8");  
              
            } catch (Exception e) {  
                System.out.println("-----"+e);  
                String string2="{\"success\":-1}";  
                  
                return string2;  
            }  
            return html;  
        }
    

    还有一个类我也贴出来

    package com.sutong.feihua.http;  
      
      
    import java.io.ByteArrayOutputStream;  
    import java.io.File;  
    import java.io.FileInputStream;  
    import java.io.FileNotFoundException;  
    import java.io.InputStream;  
    import java.io.OutputStream;  
      
      
    public class StreamTool {  
        /** 
        *  
        * @param inStream 
        * @return 
        * @throws Exception 
        */  
        public static byte[] read(InputStream inStream) throws Exception{  
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
            byte[] buffer = new byte[1024];  
            int len = 0;  
            while( (len = inStream.read(buffer)) != -1){  
                outStream.write(buffer, 0, len);  
            }  
            inStream.close();  
            return outStream.toByteArray();  
        }  
        public FileInputStream output(String fileString)  {  
            File file = new File(fileString);  
            FileInputStream fileInputStream = null;  
            try {  
                fileInputStream = new FileInputStream(file);  
            } catch (FileNotFoundException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
        }  
    return fileInputStream;  
    }  
      
      
    }  
    

    相关文章

      网友评论

      • 80c60c1a6b23:conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        修改成
        conn.setRequestProperty("Content-Type", "application/json");
        前两天还专门看了下这个协议
        我是涛锅锅:谢谢指出这个问题
      • 80c60c1a6b23:大哥,你这个变量命名真是醉了 urldizhi?url不就代表网址了嘛 如果有重复可以用uri😅
        我是涛锅锅:这以前写的方法 ,删了一些代码 所以看起来很别扭

      本文标题:android POST请求

      本文链接:https://www.haomeiwen.com/subject/sdxnjxtx.html