常见的Android 网络请求的封装;
2017-03-27 22:44
225 查看
1:Post 请求时:
public static String getWebService_POST(String parames, String str) {
String json = “”;
Basic.msg = “”;
try {
URL url = new URL(str);
// 把JSON数据转换成String类型使用输出流向服务器写
String content = String.valueOf(parames);
Log.i(“mlog”, “参数:” + content);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”); // 修改
conn.setRequestProperty(“Content-Length”, String.valueOf(content.getBytes().length) + “”);
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
// 服务器返回的响应码
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
json = readString(is);
Log.i(“mlog”, “json:” + json);
} else {
// Basic.msg=”服务器链接中断,请稍后再试”;
Log.i(“mlog”, “数据提交失败”);
}
} catch (Exception ex) {
// Basic.msg=”服务器链接中断,请稍后再试”;
Log.i(“mlog”, “Exception异常” + ex.getMessage());
}
return json;
}
public static byte[] readBytes(InputStream is) {
try {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
2)get 请求的封装:
public String getWebService_GET(String s) {
String json = “”;
try {
URL url = new URL(s);
Log.i(“mlog”, “URL:” + s);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“Charset”, “UTF-8”);
conn.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded; charset=UTF-8”);
// 服务器返回的响应码
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
json = readString(is);
Log.i(“mlog”, “json:” + json);
} else {
Log.i(“mlog”, “数据提交失败”);
}
} catch (Exception ex) {
Log.i(“mlog”, “Exception异常” + ex.getMessage());
}
return json;
}
例子:
public String GetHelpList() {
return getWebService_GET(Basic.GetHelpList_url);
}
public static String getWebService_POST(String parames, String str) {
String json = “”;
Basic.msg = “”;
try {
URL url = new URL(str);
// 把JSON数据转换成String类型使用输出流向服务器写
String content = String.valueOf(parames);
Log.i(“mlog”, “参数:” + content);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”); // 修改
conn.setRequestProperty(“Content-Length”, String.valueOf(content.getBytes().length) + “”);
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
// 服务器返回的响应码
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
json = readString(is);
Log.i(“mlog”, “json:” + json);
} else {
// Basic.msg=”服务器链接中断,请稍后再试”;
Log.i(“mlog”, “数据提交失败”);
}
} catch (Exception ex) {
// Basic.msg=”服务器链接中断,请稍后再试”;
Log.i(“mlog”, “Exception异常” + ex.getMessage());
}
return json;
}
public static byte[] readBytes(InputStream is) {
try {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String readString(InputStream is) { return new String(readBytes(is)); }
2)get 请求的封装:
public String getWebService_GET(String s) {
String json = “”;
try {
URL url = new URL(s);
Log.i(“mlog”, “URL:” + s);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“Charset”, “UTF-8”);
conn.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded; charset=UTF-8”);
// 服务器返回的响应码
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
json = readString(is);
Log.i(“mlog”, “json:” + json);
} else {
Log.i(“mlog”, “数据提交失败”);
}
} catch (Exception ex) {
Log.i(“mlog”, “Exception异常” + ex.getMessage());
}
return json;
}
例子:
public String GetHelpList() {
return getWebService_GET(Basic.GetHelpList_url);
}
public static String GetHelpList_url = basic_url + "UserCenter/GetHelpList";
相关文章推荐
- 关于android-async-http的使用,封装网络请求
- 使用android-async-http来封装Android网络请求框架
- Android之封装好的异步网络请求框架
- Android基于http封装的网络请求框架
- Android实际开发之网络请求组件的封装(OkHttp为核心)
- Android-封装post和get 网络请求
- android http——网络请求二次封装的框架设计
- android 网络请求封装,可改
- Android网络请求框架----Okhttp3完全解析(2),封装框架
- Android-Volley网络通信框架(二次封装数据请求和图片请求(包含处理请求队列和图片缓存))
- Android-Volley网络通信框架(二次封装数据请求和图片请求(包括处理请求队列和图片缓存))
- Android 的OkHttp 网络请求框架的学习封装
- 给Android封装的一个简单网络请求框架
- Android基于OkHttpUtils网络请求的二次封装
- Android Http网络请求封装
- Android 项目(一):网络请求封装(一)
- Android网络请求框架AsyncHttp二次封装
- Android网络请求代码封装
- Android 网络请求工具类封装
- Android知识点之网络底层封装:细数常用的网络请求框架