Http
Get下载
加载的网址
private String url_Get
= "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
buttonGet
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
OkHttpClient client
= new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit
.SECONDS
)
.readTimeout(20, TimeUnit
.SECONDS
)
.build();
Request request
= new Request.Builder()
.url(url_Get
)
.get()
.build();
Call call
= client
.newCall(request
);
call
.enqueue(new Callback() {
@Override
public void onFailure(Call call
, final IOException e
) {
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "失败原因" + e
.getMessage(), Toast
.LENGTH_SHORT
).show();
}
});
}
@Override
public void onResponse(Call call
, Response response
) throws IOException
{
ResponseBody responseBody
= response
.body();
final String string
= responseBody
.string();
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "成功" + string
, Toast
.LENGTH_SHORT
).show();
}
});
}
});
}
});
Post下载
下载的网址
private String url_Post
= "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&";
buttonPost
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
OkHttpClient client
= new OkHttpClient.Builder()
.readTimeout(20, TimeUnit
.SECONDS
)
.connectTimeout(20, TimeUnit
.SECONDS
)
.build();
FormBody formBody
= new FormBody.Builder()
.add("page", "1")
.build();
Request request
= new Request.Builder()
.url(url_Post
)
.post(formBody
)
.build();
Call call
= client
.newCall(request
);
call
.enqueue(new Callback() {
@Override
public void onFailure(Call call
, final IOException e
) {
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "失败:" + e
.getMessage(), Toast
.LENGTH_SHORT
).show();
}
});
}
@Override
public void onResponse(Call call
, final Response response
) throws IOException
{
final String s
= response
.body().string();
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "成功:" + s
, Toast
.LENGTH_SHORT
).show();
}
});
}
});
}
});
下载
网址
private String url_downLoad
= "http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4";
buttonDownLoad
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
Log
.i(TAG
, "onClick: drxcgvhbnk");
OkHttpClient client
= new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit
.SECONDS
)
.readTimeout(20, TimeUnit
.SECONDS
)
.build();
final Request request
= new Request.Builder()
.get()
.url(url_downLoad
)
.build();
Call call
= client
.newCall(request
);
call
.enqueue(new Callback() {
@Override
public void onFailure(Call call
, final IOException e
) {
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "失败:" + e
.getMessage(), Toast
.LENGTH_SHORT
).show();
}
});
}
@Override
public void onResponse(Call call
, Response response
) throws IOException
{
ResponseBody body
= response
.body();
Log
.i(TAG
, "onResponse: " + response
.code());
long length
= body
.contentLength();
InputStream inputStream
= body
.byteStream();
int len
= 0;
byte[] bytes
= new byte[1024];
int currentLen
= 0;
FileOutputStream fileOutputStream
= new FileOutputStream(new File("/storage/emulated/0/yy.mp4"));
while ((len
= inputStream
.read(bytes
)) != -1) {
fileOutputStream
.write(bytes
, 0, len
);
currentLen
+= len
;
final int l
= (int) (currentLen
* 100 / length
);
handler
.post(new Runnable() {
@Override
public void run() {
progress
.setMax(100);
progress
.setProgress(l
);
}
});
}
}
});
}
});
上传
网址 sd地址
private String url_upload
="http://169.254.119.49/mogu/";
private String uploadImage
="/storage/emulated/0/yy.mp4";
buttonUpload
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
OkHttpClient client
= new OkHttpClient.Builder()
.readTimeout(20, TimeUnit
.SECONDS
)
.connectTimeout(20, TimeUnit
.SECONDS
)
.build();
RequestBody requestBody
= RequestBody
.create(MediaType
.parse("Media/mp4"), new File(uploadImage
));
MultipartBody multipartBody
= new MultipartBody.Builder()
.setType(MultipartBody
.FORM
)
.addFormDataPart("file","mogu.mp4",requestBody
)
.build();
final Request request
= new Request.Builder()
.url(url_upload
)
.post(multipartBody
)
.build();
Call call
= client
.newCall(request
);
call
.enqueue(new Callback() {
@Override
public void onFailure(Call call
, final IOException e
) {
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "失败:"+e
.getMessage(), Toast
.LENGTH_SHORT
).show();
}
});
}
@Override
public void onResponse(Call call
, Response response
) throws IOException
{
final String string
= response
.body().string();
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "成功:"+string
, Toast
.LENGTH_SHORT
).show();
}
});
}
});
}
});