zoukankan      html  css  js  c++  java
  • Android WebAPIOperator

    package com.example.myapplication2.models.CommonClasses;
    import org.json.JSONObject;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLDecoder;
    import com.google.gson.*;
    
    public class WebAPIOperator {
        static String ip="192.168.56.192";
        static String port="18011";
        static String baseUri="http://"+ip+":"+port;
    
    //    HttpClient httpClient;
        static HttpURLConnection huc;
    
        public WebAPIOperator() {
    
        }
        public static String DoGet(String locationStr,String subStr){
            String str="";
            try {
                URL url=new URL(locationStr+subStr);
                huc=(HttpURLConnection)url.openConnection();
                huc.connect();
                // 读取响应
                BufferedReader reader = new BufferedReader(new
                        InputStreamReader(huc.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = URLDecoder.decode(lines, "utf-8");
                    sb.append(lines);
                }
                System.out.println(sb);
                reader.close();
                str=sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                huc.disconnect();
                return str;
            }
        }
        public static String DoGet(String locationStr){
            String str="";
            try {
                URL url=new URL(baseUri+locationStr);
                huc=(HttpURLConnection)url.openConnection();
                huc.connect();
                // 读取响应
                BufferedReader reader = new BufferedReader(new
                        InputStreamReader(huc.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = URLDecoder.decode(lines, "utf-8");
                    sb.append(lines);
                }
                System.out.println(sb);
                reader.close();
                str=sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                huc.disconnect();
                return str;
            }
        }
        public static String DoPost(String locationStr,Object obj){
            String str="";
            try {
                URL url=new URL(baseUri+locationStr);
                huc=(HttpURLConnection)url.openConnection();
                huc.setDoOutput(true);
                huc.setDoInput(true);
                huc.setRequestMethod("POST");
    //            huc.setRequestProperty("Content-Type","plain/text; charset=UTF-8");
                huc.setRequestProperty("Content-Type","application/json; charset=UTF-8");
                // 设置通用的请求属性
                huc.setRequestProperty("accept", "*/*");
                huc.setRequestProperty("connection", "Keep-Alive");
    
                huc.setUseCaches(false);
                huc.setInstanceFollowRedirects(true);
    
                huc.connect();
    //            int responseCode=huc.getResponseCode();
    //            System.out.println("responseCode:"+responseCode);
    //            Object o=huc.getOutputStream();
    
    //            OutputStream out = new BufferedOutputStream(huc.getOutputStream());
    //            writeStream(out);
    //
    //            InputStream in = new BufferedInputStream(huc.getInputStream());
    //            readStream(in);
                DataOutputStream out = new
                        DataOutputStream(huc.getOutputStream());
    //            JSONObject obj = new JSONObject();
    //            String json = java.net.URLEncoder.encode(obj.toString(), "utf-8");
                String json=new Gson().toJson(obj);
                out.writeBytes(json);
                out.flush();
                out.close();
                // 读取响应
                BufferedReader reader = new BufferedReader(new
                        InputStreamReader(huc.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = URLDecoder.decode(lines, "utf-8");
                    sb.append(lines);
                }
                System.out.println(sb);
                reader.close();
                str=sb.toString();
                return str;
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                huc.disconnect();
                return str;
            }
    
        }
    }
    

      

  • 相关阅读:
    #2051:Bitset(进制转化)
    #2054:A == B ?(水题坑人)
    #2045:不容易系列之三LELE的RPG难题(dp递推)
    #2037:今年暑假不AC
    #2036:改革春风吹满地
    OJ中的语言选项里G++ 与 C++的区别
    如何在CSDN上如何快速转载博客
    Python之路(第八篇)Python内置函数、zip()、max()、min()
    Python编程笔记(第一篇)Python基础语法
    Python之路(第七篇)Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数
  • 原文地址:https://www.cnblogs.com/nick-jd/p/12421109.html
Copyright © 2011-2022 走看看