Android开发,get ,post请求api
public class InvokWebApi {
private static String srcUrl = "172.16.1.2:8989";
public static String getUrl() {
return srcUrl;
}
public static void setUrl(String UrlStr) {
srcUrl = UrlStr;
}
public static String GetParamsJson(String ControllerName, String ActionName, String Parameter) throws Exception{
String resultString = null;
HttpURLConnection conn = null;
InputStream inputStream = null;
ByteArrayOutputStream bos = null;
try {
String urlStr = "http://" + srcUrl +"/" + ControllerName + "/" + ActionName + "?" + Parameter;
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setDoOutput(false);
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
if (null != conn.getHeaderField("Content-Encoding")) {
inputStream = new GZIPInputStream(conn.getInputStream());
} else {
inputStream = conn.getInputStream();
}
bos = new ByteArrayOutputStream();
byte[] buffer = new byte[10240];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.flush();
byte[] resultByte = bos.toByteArray();
resultString = new String(resultByte);
} else {
resultString = "";
}
} catch (Exception ex) {
throw ex;
} finally {
if (conn != null) {
conn.disconnect();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
throw e;
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
throw e;
}
}
}
return resultString;
}
public static String SendPostMessage(String ControllerName, String ActionName, String params) throws Exception{
StringBuffer buffer = new StringBuffer();
try {
if (params != null && !params.isEmpty()) {
buffer.append(params);
}
byte[] mydata = buffer.toString().getBytes();
String urlStr = "http://" + srcUrl + "/" + ControllerName + "/" + ActionName;
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", String.valueOf(mydata.length));
connection.connect();
OutputStream outputStream = connection.getOutputStream();
outputStream.write(mydata, 0, mydata.length);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
return changeInputeStream(connection.getInputStream(), "utf-8");
}else{
return GsonUtils.ToJSON(new OperReturnInfo(0,responseCode+""));
}
} catch (UnsupportedEncodingException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
private static String changeInputeStream(InputStream inputStream, String encode) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = "";
if (inputStream != null) {
try {
while ((len = inputStream.read(data)) != -1) {
data.toString();
outputStream.write(data, 0, len);
}
result = new String(outputStream.toByteArray(), encode);
outputStream.flush();
} catch (IOException e) {
throw e;
}
}
return result;
}
}