android - 使用带有 multipart 的 asyntask 上传图像不起作用?
问题描述
我想用一张图片发布三个字符串字段,我在下面的代码中尝试过它在没有图片的情况下工作
下面的方法我在 doInBackground 中调用 asyntask
private void fileUpload() {
Log.e("file_upl","file_upl started");
int serverResponse;
Bitmap b = BitmapFactory.decodeFile("/sdcard/screen_shot.png");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 0, baos);
try {
HttpClientcreaed client = new HttpClientcreaed(url+"/api/v1/screens/screenshot_upload/");
HashMap<String,Object> hashMap_post = new HashMap<>();
hashMap_post.put("device_id",deviceId);
hashMap_post.put("random_number",randomNumber);
hashMap_post.put("type","scrnshot");
hashMap_post.put("file", baos.toByteArray()); //i think here
// is problem how to sent image bye...without image its working
serverResponse = client.connectForMultipart(hashMap_post);
Log.e("checkingresponceresults","&&&& "+serverResponse);
}
catch(Throwable t) {
t.printStackTrace();
Log.e("exception","excep " + t.toString());
}
}
这是 我调用的Httpcliendcread 类中的方法
public int connectForMultipart(HashMap<String,Object> postDataParams) throws Exception {
con = (HttpURLConnection) (new URL(url)).openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Connection", "Keep-Alive"); //application/x-www-form-urlencoded
//con.setRequestProperty("Content-Type", "multipart/x-www-form-urlencoded; boundary=" + boundary);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
String response = "";
if ( con.getResponseCode() == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}
Log.e("responchecking","&&&& "+response);
return con.getResponseCode();
/* con.connect();
os = con.getOutputStream();*/
}
这是附加 url 的另一种方法
private String getPostDataString(HashMap<String, Object> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, Object> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode((String) entry.getValue(), "UTF-8"));
}
return result.toString();
}
我已经搜索了谷歌,但我没有得到任何解决方案......
我检查了邮递员的工作
以下是错误:
例外 java.lang.ClassCastException: byte[] 不能转换为 java.lang.String
提前致谢
解决方案
推荐阅读
- javascript - 为什么JS说这个函数不是函数
- html - Bootstrap 导航栏问题,让按钮向右拉
- azure-data-explorer - 使用按 Z 排序的 top N 对 X 和 Y 进行汇总
- reactjs - React build 使用 Django 显示白页
- python - tkinter:通过单击移动图像
- css - 如何根据sass中的父属性计算子属性
- javascript - 使用 Javascript (VanillaJS) 添加点击事件并显示/隐藏下一个元素
- c# - 尝试连接到另一台计算机上存储的数据库时出现“等待操作超时”错误
- java - Spring gRPC - 如何使用正文信息拦截异常
- android - ForYouFragment 片段不更新