• 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;
            }
    
        }
    }
    

      

  • 相关阅读:
    Opengl绘制我们的小屋(二)第一人称漫游
    C# this.Invoke和this.BeginInvoke 最简单的写法
    C# 递归模型定义。赋值
    .net Core 2.1 后 Session保存,新页面获取不到值
    .net core mvc 错误信息显示 ModelState.AddModelError
    .net Core 依赖注入 Add********说明
    C# 中2个问号的作用。C#的??代表是什么意思
    asp.net mvc 加三层架构 完美搭配
    C# DataTable.Compute()用法
    C# DateTime判断时间
  • 原文地址:https://www.cnblogs.com/nick-jd/p/12421109.html
Copyright © 2020-2023  润新知