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全部内容,希望文章能够帮你解决Android WebAPIOperator所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)