/**
* 上传头像....上传成功之后进行显示
* @param view
*/
public void upload(View view) {
//url上传的路径 file上传的文件 fileName上传文件的名称(保存在服务器上的名字)
//params 存放除了file以外需要的参数,没有其他参数null callBack回调
File file = new File(Environment.getExternalStorageDirectory(),"abc.jpg");
Map<String, String> params = new HashMap<>();
params.put("uid","3010");
OkHttp3Util.uploadFile("https://www.zhaoapi.cn/file/upload", file, "dash.jpg", params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
//此时上传成功....获取用户信息
OkHttp3Util.doGet("https://www.zhaoapi.cn/user/getUserInfo?uid=3010", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//子线程
if (response.isSuccessful()){
String json = response.body().string();
//解析出icon字段...使用glide加载上传到网络上这个图片
final UserBean userBean = new Gson().fromJson(json, UserBean.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
Glide.with(MainActivity.this).load((String) userBean.getData().getIcon()).into(imageView);
}
});
}
}
});
}
}
});
}
}
* 上传头像....上传成功之后进行显示
* @param view
*/
public void upload(View view) {
//url上传的路径 file上传的文件 fileName上传文件的名称(保存在服务器上的名字)
//params 存放除了file以外需要的参数,没有其他参数null callBack回调
File file = new File(Environment.getExternalStorageDirectory(),"abc.jpg");
Map<String, String> params = new HashMap<>();
params.put("uid","3010");
OkHttp3Util.uploadFile("https://www.zhaoapi.cn/file/upload", file, "dash.jpg", params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
//此时上传成功....获取用户信息
OkHttp3Util.doGet("https://www.zhaoapi.cn/user/getUserInfo?uid=3010", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//子线程
if (response.isSuccessful()){
String json = response.body().string();
//解析出icon字段...使用glide加载上传到网络上这个图片
final UserBean userBean = new Gson().fromJson(json, UserBean.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
Glide.with(MainActivity.this).load((String) userBean.getData().getIcon()).into(imageView);
}
});
}
}
});
}
}
});
}
}
<Button
android:onClick="upload"
android:text="post上传"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image_touxiang"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />