最近要用到想服务器上传文件数据,先抄个作业,明天研究之后再做定论:
public void btnUpload(View v){// newName = "base.jpg"; newName = "VideoTest.mp4"; uploadFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+newName; uploadFile = new File(uploadFilePath); if(!uploadFile.exists()) { Toast.makeText(MainActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show(); return; } actionUrl = "http://182.92.152.12:10898/check_again";// actionUrl = "http://server.jeasonlzy.com/OkHttpUtils/upload"; Map<String,String> params = new HashMap<>(); params.put("info","panbaomeng"); OkHttpUtils.post() .addFile("newName",newName,uploadFile) .url(actionUrl) .params(params) .build() .execute(new MyStringCallback()); } public class MyStringCallback extends StringCallback { @Override public void onBefore(Request request, int id) { setTitle("loading..."); } @Override public void onAfter(int id) { setTitle("Sample-okHttp"); } @Override public void onError(Call call, Exception e, int id) { e.printStackTrace(); Log.e("onError:",e.getMessage()); } @Override public void onResponse(String response, int id) { Log.e("onResponse", response); switch (id) { case 100: Toast.makeText(MainActivity.this, "http", Toast.LENGTH_SHORT).show(); break; case 101: Toast.makeText(MainActivity.this, "https", Toast.LENGTH_SHORT).show(); break; } } @Override public void inProgress(float progress, long total, int id) {// Log.e(TAG, "inProgress:" + progress);// mProgressBar.setProgress((int) (100 * progress)); } }
Android文件上传实践
本文介绍了一个使用OkHttp在Android应用中实现文件上传的例子。通过指定文件路径和服务器URL,可以将本地文件上传到服务器,并接收服务器响应。
1239

被折叠的 条评论
为什么被折叠?



