我将文件从Android上传到Heroku(PlayFramework 2.2 Java)
我使用MultipartEntity和HttpPost上传了一张小图片。
我的请求被服务器立即收到并处理,但是response.execute(..)需要20秒才能终止。
我用过这个上传文件的装载程序 http://toolongdidntread.com/android/android-multipart-post-with-progress-bar/
文件立即上传。服务器获取它并立即发送响应。
有什么想法吗?
final AsyncTask task=new AsyncTask<Void, Integer, String>() {
public ProgressBar uploadSeekBar= (ProgressBar) findViewById(R.id.progressBar);
int size;
@Override
protected String doInBackground(Void... params) {
String result = "";
final String url = ResourcesUtil.SERVER_URL+"/submit/";
HttpPost httppost = new HttpPost(url);
CustomMultiPartEntity multipartEntity = new CustomMultiPartEntity(new CustomMultiPartEntity.ProgressListener()
{
@Override
public void transferred(long num)
{
publishProgress((int) num);
}
});
try {
HttpClient client = new DefaultHttpClient();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BitmapFactory.decodeFile(imageFilePath).compress(Bitmap.CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, imageFilePath); // Your image file
multipartEntity.addPart("picture", bab);
multipartEntity.addPart("totem.id", new StringBody(totemId));
multipartEntity.addPart("totem_name", new StringBody(totemName));
multipartEntity.addPart("lat", new StringBody(ScanActivity.location.getLatitude()+""));
multipartEntity.addPart("lon", new StringBody(ScanActivity.location.getLongitude()+""));
multipartEntity.addPart("message", new StringBody(((EditText) findViewById(R.id.flashAnswer)).getText().toString()));
multipartEntity.addPart("name", new StringBody(((EditText) findViewById(R.id.flasher_name)).getText().toString()));
multipartEntity.addPart("mail", new StringBody(((EditText) findViewById(R.id.flasher_email)).getText().toString()));
httppost.setEntity(multipartEntity);
size= (int) multipartEntity.getContentLength();
uploadSeekBar.setMax(size);
uploadSeekBar.setProgress(0);
uploadSeekBar.setVisibility(View.VISIBLE);
Log.e("request","begin");
HttpResponse response = client.execute(httppost);
Log.e("request","end");
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode()!=200){
return response.getStatusLine().getStatusCode()+"";
}
result = EntityUtils.toString(response.getEntity());
entity.consumeContent();
client.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Loggr.log("Error request flash", "error flash");
try {
multipartEntity.writeTo(bytes);
} catch (IOException e1) {
e1.printStackTrace();
Loggr.log("Error logging flash form", "error flash");
}
String content = bytes.toString();
Loggr.logData("Error flash submit ", "error flash", e.toString());
Loggr.logData("Error flash submit data", "error flash", content);
}
return result;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
uploadSeekBar.setProgress(values[0]);
Log.e("progress",values[0]+"");
}
}.execute();
LOGCAT:
04-23 09:59:18.355 3580-3798/com.wimha.app E/request﹕ begin
04-23 09:59:18.746 3580-3580/com.wimha.app E/progress﹕ 2
04-23 09:59:18.800 3580-3580/com.wimha.app E/progress﹕ 34
......
04-23 09:59:19.035 3580-3580/com.wimha.app E/progress﹕ 11020
04-23 09:59:19.035 3580-3580/com.wimha.app E/progress﹕ 11022
04-23 09:59:30.757 3580-3798/com.wimha.app E/request﹕ end
编辑:其他尝试使用LoopJ库
private void sendRequest(final String totemId, final String totemName) {
final ProgressBar uploadSeekBar= (ProgressBar) findViewById(R.id.progressBar);
final String url = ResourcesUtil.SERVER_URL+"/submit/";
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(20000);
File myFile = new File(imageFilePath);
RequestParams params = new RequestParams();
try {
params.put("picture", myFile);
} catch(FileNotFoundException e) {
Loggr.log("Error request flash file not found", "error flash");
}
params.put("totem.id", totemId);
params.put("totem_name", totemName);
params.put("lat", ScanActivity.location.getLatitude()+"");
params.put("lon", ScanActivity.location.getLongitude()+"");
params.put("message", ((EditText) findViewById(R.id.flashAnswer)).getText().toString());
params.put("name", ((EditText) findViewById(R.id.flasher_name)).getText().toString());
params.put("mail", ((EditText) findViewById(R.id.flasher_email)).getText().toString());
client.post(this,url,params,new AsyncHttpResponseHandler() {
public boolean setSize;
@Override
public void onStart() {
super.onStart();
Loggr.log("Submitting","created flash");
uploadSeekBar.setProgress(0);
uploadSeekBar.setVisibility(View.VISIBLE);
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
super.onSuccess(statusCode, headers, responseBody);
Loggr.log("Submitted","created flash");
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(findViewById(R.id.flasher_email).getWindowToken(), 0);
handleResponse(new String(responseBody));
Log.e("success",new String(responseBody));
}
@Override
public void onProgress(int bytesWritten, int totalSize) {
super.onProgress(bytesWritten, totalSize);
Log.e("progress",bytesWritten+"");
if(!setSize){
setSize=true;
uploadSeekBar.setMax(totalSize);
}
uploadSeekBar.setProgress(bytesWritten);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
super.onFailure(statusCode, headers, responseBody, error);
Log.e("fail",statusCode+" "+responseBody.toString());
Loggr.logData("Error flash submit ", "error flash", responseBody.toString());
}
});
}
Logcat :
04-22 20:46:39.832 28314-28314/com.wimha.app E/progress﹕ 968
04-22 20:46:39.843 28314-28314/com.wimha.app E/progress﹕ 1175
04-22 20:46:39.843 28314-28314/com.wimha.app E/progress﹕ 5271
04-22 20:46:39.847 28314-28314/com.wimha.app E/progress﹕ 9367
04-22 20:46:39.859 28314-28314/com.wimha.app E/progress﹕ 13463
04-22 20:46:39.859 28314-28314/com.wimha.app E/progress﹕ 17559
04-22 20:46:39.859 28314-28314/com.wimha.app E/progress﹕ 21655
04-22 20:46:39.859 28314-28314/com.wimha.app E/progress﹕ 24973
04-22 20:46:39.863 28314-28314/com.wimha.app E/progress﹕ 24975
04-22 20:46:39.863 28314-28314/com.wimha.app E/progress﹕ 25011
04-22 20:46:50.461 28314-28314/com.wimha.app E/progress﹕ 74
04-22 20:46:50.574 28314-28332/com.wimha.app I/System.out﹕ URL: http://post.loggr.net/1/logs/totemwimha2/events
04-22 20:46:50.625 28314-28332/com.wimha.app I/System.out﹕ PostStr: text=Submitted&tags=created+flash+android&source=android&apikey=696d22c04aba45b6a1fc2e67e70b6d60
04-22 20:46:50.629 28314-28314/com.wimha.app E/success﹕ {"first_flash":"true","postDate":"2014-04-22","timestamp":"1398192401545"}
答案 0 :(得分:0)
尝试添加此内容:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg"); // Your image file
然后,您发送文件
multipartEntity.addPart("picture", bab);
对不起我的简短回答,如果你需要最好的解释,我会更好地回答