androidokhttp上传文件,AndroidStudio上传文件实例_OKHTTP

更新时间:2023-06-14 18:01:15 阅读: 评论:0

androidokhttp上传⽂件,AndroidStudio上传⽂件实例_OKHTTP 效果(服务器是⾃⼰的,地址⾃⼰找)
image.png
思路
1、添加依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation "com.github.bumptech.glide:glide:4.8.0"
implementation 'fit2:converter-gson:2.4.0'
2、添加权限
⽹络、读写权限。
3、XML布局
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:id="@+id/upload_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上传" />
android:id="@+id/upload_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/upload_bt"
android:gravity="center"
android:text="显⽰⽂本"
android:textSize="18sp" />
android:id="@+id/upload_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/upload_tv"
郑洞国
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background" />
4、动态权限获取
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)==
PackageManager.PERMISSION_GRANTED){
try {
upLodeFile();
} catch (IOException e) {
e.printStackTrace();
}
}el {
}
5、OKHTTP上传⽂件
//创建OK
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
//请求体
RequestBody requestBody = ate(MediaType.par("image/png"), file);
MultipartBody body = new MultipartBody.Builder()
.tType(MultipartBody.FORM)
.addFormDataPart("key", "1") //post请求Key,value
什么东西补锌
.addFormDataPart("file", Name(),requestBody) //post请求Key,value
.build();
//构建请求
Request request = new Request.Builder()
// .url("yun918/study/public/index.php/file_upload.php")
.url("yun918/study/public/file_upload.php")
.post(body)
.build();
//call对象
Call call = wCall(request);
//call执⾏请求
@Override
public void onFailure(Call call, IOException e) {
Log.e("tag", "onFailure: "+e.getMessage());
}
@Override
public void onRespon(Call call, Respon respon) throws IOException {
String json = respon.body().string();
final UpLoadBean upLoadBean = new Gson().fromJson(json, UpLoadBean.class);
if (!TextUtils.isEmpty(json)){
int code = Code();
String str = String.valueOf(code);
if (str.equals("200")){
元宵节活动报道runOnUiThread(new Runnable() {
@Override
public void run() {
uploadTv.Res());
Glide.with(MainActivity.this).Data().getUrl()).into(uploadImage); }
});
}el {
Toast.makeText(MainActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
}
}
}
});
完整代码
MainActivity草莓农药
uploadfile;
import android.Manifest;
t.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
TextUtils;
左车轮import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; app.ActivityCompat;
ontent.ContextCompat;
import com.bumptech.glide.Glide;
le.gson.Gson;
import java.io.File;
品字形结构的字import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
什么是创业机会
import okhttp3.Respon;移动宽带路由器设置
public class MainActivity extends AppCompatActivity { private Button uploadBt;
private TextView uploadTv;
private ImageView uploadImage;
@Override
protected void onCreate(Bundle savedInstanceState) { Create(savedInstanceState);
tContentView(R.layout.activity_main);
initView();
uploadBt.tOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
checkPermission();//检查危险权限
}
});
}
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)==
PackageManager.PERMISSION_GRANTED){
try {
upLodeFile();
} catch (IOException e) {
e.printStackTrace();
}
}el {
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.e("tag", "onRequestPermissionsResult: "+requestCode );
if (grantResults!=null&&grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "⽤户授权", Toast.LENGTH_SHORT).show();
try {
upLodeFile();
} catch (IOException e) {
e.printStackTrace();
}
}el {
Toast.makeText(this, "⽤户未授权", Toast.LENGTH_SHORT).show();
}
}
private File file;
private void upLodeFile() throws IOException {
file = new ExternalStorageDirectory() + "/Pictures/Screenshots/a.png");//获取⽬录(不加后⾯的字符串是你的根⽬录+后⾯是继续找的意思)
getFileLog();
//ok上传

本文发布于:2023-06-14 18:01:15,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1038423.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:权限   参数   获取   效果   上传   实例
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图