在依賴中可以用
implementation 'com.yanzhenjie.nohttp:okhttp:+'
//下載文件時需要在依賴:implementation files('libs/okio-1.13.0.jar')
或者用jar包
implementation files('libs/okhttp-3.8.0.jar')
implementation files('libs/okio-1.13.0.jar')
"GET"方式請求網(wǎng)絡(luò)
public void getDataByOKGet() {
OkHttpClient okHttpClient = new OkHttpClient();//把客戶端new出來
Request.Builder builder = new Request.Builder();//請求建立對象
try {
Request request = builder
.get()//"GET"方式請求網(wǎng)絡(luò)
.url("http://apicloud.mob.com/environment/query?key=26b2b13b4b440&city="
+ URLEncoder.encode("哈爾濱","UTF-8")
+"&province="
+URLEncoder.encode("黑龍江","UTF-8"))
.build();//請求對象建立請求
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {//入隊
@Override
public void onFailure(Call call, IOException e) {
//當失敗時
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
// 當響應(yīng)時
runOnUiThread(new Thread(){//在主線程更新UI
@Override
public void run() {
try {
mTextView.setText(response.body().string());//從網(wǎng)絡(luò)上獲取的就是JSON
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
"POST"方式請求網(wǎng)絡(luò)(所以需要寫一個模板,來產(chǎn)生一個請求主體)
public void getDataByOKPost(){
try {
OkHttpClient okHttpClient = new OkHttpClient();//OK客戶端對象
FormBody.Builder build = new FormBody.Builder();//建設(shè)模板
RequestBody requestBody = build
.add("city",URLEncoder.encode("哈爾濱","UTF-8"))
.add("province",URLEncoder.encode("黑龍江","UTF-8"))
.add("key",URLEncoder.encode("26b2b13b4b440","UTF-8"))
.build();//用模板.build返回請求主體
Request.Builder builder = new Request.Builder();//建立請求對象
Request request = builder
.post(requestBody)//"POST"方式請求網(wǎng)絡(luò)(所以需要寫一個模板,來產(chǎn)生一個請求主體)
.url("http://apicloud.mob.com/v1/postcode/pcd")
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String s = response.body().string();
runOnUiThread(new Thread(){
@Override
public void run() {
try {
mTextView.setText(s);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
顯示圖片下載
public void getDataByOKDown(){
try {
OkHttpClient okHttpClient = new OkHttpClient();
FormBody.Builder build = new FormBody.Builder();
RequestBody requestBody = build.build();
Request.Builder builder = new Request.Builder();
Request request = builder
.post(requestBody)
.url("http://pic2.sc.chinaz.com/files/pic/pic9/201808/zzpic13391.jpg")
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
InputStream is = response.body().byteStream();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
byte buffer[] = new byte[512];
int length = -1;
while( (length = is.read(buffer)) != -1 ){
bs.write(buffer,0,length);
bs.flush();
}
final File file = new File(
Environment.getExternalStorageDirectory()+"/12331710.jpg");
FileOutputStream fs = new FileOutputStream(file);
byte data[] = bs.toByteArray();
Log.d("data",data.length+"");
fs.write(data,0,data.length);
fs.flush();
is.close();
bs.close();
fs.close();
runOnUiThread(new Thread(){
@Override
public void run() {
Log.d("data",file.getAbsolutePath());
mImageView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
}
});
}
});
} catch (Exception e) {
e.printStackTrace();
}
}