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

    }
}

  

Android WebAPIOperator

上一篇:Spring Boot 学习笔记(一)基础依赖配置、@SpringBootApplication 注解、官方文档


下一篇:Qt 窗口随鼠标移动