HttpConnection
1.HttpConnection 用于接受和发送网络数据
网络操作必须新开个子线程执行,否则会出现 ANR(Application Not Response) 应用无响应异常
Get:
/**
* 通过HttpURLConnection 获取输入流
*/
public static InputStream getInputStreamFromWebSite(String path) {
InputStream inputStream = null;
try {
//创建URL对象指定我们要访问的网址(路径)
URL url = new URL(path);
//创建HttpurlConnection 对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置发送 get 请求
conn.setRequestMethod("GET");
//设置请求超时时间
conn.setConnectTimeout(5000);
//访问成功
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = conn.getInputStream();
}
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
Post: 通过请求提的方式把数据传给服务器(以流的形式)
/**
* 通过HttpURLConnection 上传数据
*/
public static InputStream postDataToWebAPI(String path,String data) {
InputStream inputStream = null;
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//设置请求头信息
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",String.valueOf(data.length()));
//将拼接好的数据已流的形式提交给数据库
conn.setDoOutput(true);//设置一个标记,允许输出
conn.getOutputStream().write(data.getBytes());
int code=conn.getResponseCode();
Tools.log("ResponseCode:"+String.valueOf(code));
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
//获取返回结果
inputStream = conn.getInputStream();
}
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}
重点
//将拼接好的数据已流的形式提交给数据库
conn.setDoOutput(true);//设置一个标记,允许输出
conn.getOutputStream().write(data.getBytes());
2. okHttp3
(1)创建 get post 方法
/**
* 通过okHttp3 执行 get
*/
public static void getDataByOkHttp(String path, Callback callback) {
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
Request request = new Request.Builder()
.url(path)
.build();
//new call
okhttp3.Call call = mOkHttpClient.newCall(request);
//请求加入调度
call.enqueue(callback);
}
/**
* 通过okHttp3 执行 post
*/
public static void postDataToServerByOkHttp(String url, RequestBody formBody, Callback callback) {
OkHttpClient mOkHttpClient=new OkHttpClient();
Request request=new Request.Builder()
.url(url)
.post(formBody) //数据
.build();
mOkHttpClient.newCall(request).enqueue(callback); //回调
}
(2)调用
onResponse() 方法时执行在子线程当中的
NetUtils.getDataByOkHttp(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result=response.body().string();
}
});
FormBody body = new FormBody.Builder()
.add("Name", name).add("PhoneNumber", phone).add("Gender", gender).add("Passwords", pwd)
.add("RoleId", "1").add("Image", image).build();
NetUtils.postDataToServerByOkHttp(APIPaths.USER_REGISTER, body, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(TAG, "onResponse: " + response.body().string());
}
});
文章部分内容摘自网络和图书,如有侵权,请联系我。