我正在尝试使用http post request将视频上传到服务器。
当我调试并看到http响应时,它显示200 OK
我的服务电话
但在此之后
String responseStr = EntityUtils.toString(resEntity).trim();
responseStr返回{“Success”:false,“Data”:“Index超出范围。必须是非负数且小于集合的大小。\ r \ nParameter name:index”,“Message” : “测试”, “ID”:0, “电子邮件”:空, “URL”:空, “imgUrl的”:空} 和视频无法上传。
我的代码
获取视频详情
int mychunkSize = 100 * 1024;
videoLength = String.format(
"%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(duration),
TimeUnit.MILLISECONDS.toSeconds(duration)
- TimeUnit.MINUTES
.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(duration)));
realPath = getRealPathFromURI(imageFileUri2);
long size1 = realPath.length();
size = Long.toString(size1);
long chunks1 = size1 < mychunkSize ? 1
: (realPath.length() / mychunkSize);
chunks = Long.toString(chunks1);
file1 = new File(realPath);
long length1 = file1.length();
length1 = length1 / 1024;
filename = file1.getName().toString();
eventID = user.getnEventID();
userID = user.getnUserID();
commentMsg = "videotesting";
上传到服务器
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("uploaded", new StringBody(realPath));
HttpClient httpclient1 = new DefaultHttpClient();
HttpPost httppost1 = new HttpPost(
"http://capmem.omsoftware.co/Event/UploadVideo");
try {
// Add your data
List<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>(
2);
nameValuePairs1.add(new BasicNameValuePair("callback",
"localJsonpCallback"));
nameValuePairs1.add(new BasicNameValuePair("filename",
filename));
nameValuePairs1.add(new BasicNameValuePair("ext", "mp4"));
nameValuePairs1.add(new BasicNameValuePair("totalsize",
size));
nameValuePairs1.add(new BasicNameValuePair("EventID",
eventID));
nameValuePairs1
.add(new BasicNameValuePair("UserID", userID));
nameValuePairs1.add(new BasicNameValuePair("comment",
commentMsg));
nameValuePairs1.add(new BasicNameValuePair("VideoLength",
videoLength));
nameValuePairs1
.add(new BasicNameValuePair("chunk", chunks));
httppost1.setEntity(new UrlEncodedFormEntity(
nameValuePairs1));
// Execute HTTP Post Request
HttpResponse response = httpclient1.execute(httppost1);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity)
.trim();
// you can add an if statement here and do other actions
// based on the response
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
这里“响应”给出200 OK,但是 “responseStr”返回{“Success”:false,“Data”:“Index超出范围。必须是非负数且小于集合的大小。\ r \ nParameter name:index”,“Message”:“测试”, “标识”:0, “电子邮件”:空, “URL”:NULL, “imgUrl的” 日期null}
我需要在循环中添加块,使用offset在循环中执行,直到总大小和偏移量相等。
请帮助..
答案 0 :(得分:0)
您错误地使用路径长度来计算要发送的数据块数。
改变这个:
long chunks1 = size1 < mychunkSize ? 1
: (realPath.length() / mychunkSize);
要:
long chunks1 = size1 < mychunkSize ? 1
: (size1 / mychunkSize);