我正在尝试使用android-async-http库将异步文件上传到服务器。回调在我调用上传的方法中。但是,尝试从AsyncTask类调用该方法时出现错误。任何人都可以找到错误的地方吗? 这是我的上传方法:
public void upload(String title, String genre, String description, String songuri, int accountId) {
File song = new File(songuri);
try {
String url = Uri.parse(getResources().getString(R.string.audio_upload_url))
.buildUpon()
.appendQueryParameter("title", title)
.appendQueryParameter("tags", genre)
.appendQueryParameter("description", description)
.appendQueryParameter("accountId", String.valueOf(accountId))
.build().toString();
AsyncHttpResponseHandler httpResponseHandler = createHTTPResponseHandler();
RequestParams params = new RequestParams();
// this path could be retrieved from library or camera
//String imageFilePath = "/storage/sdcard/DCIM/Camera/IMG.jpg";
//params.put("data", new File(imageFilePath));
params.put("song", song);
AsyncHttpClient client = new AsyncHttpClient();
client.post(url, params, httpResponseHandler);
} catch (IOException e) {
e.printStackTrace();
}
}
以下是我初始化AsyncHttpResponseHandler类的方法。
public AsyncHttpResponseHandler createHTTPResponseHandler() {
AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {
@Override
public void onStart() {
super.onStart();
}
@Override
public void onProgress(int position, int length) {
super.onProgress(position, length);
// progressBar.setProgress(position);
//progressBar.setMax(length);
}
@Override
public void onFinish() {
super.onFinish();
}
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
}
};
return handler;
}
在我的AsyncTask类中,这就是我调用上传方法的方式。
@Override
protected UploadFragment doInBackground(UploadFragment... params) {
String title = titleEt.getText().toString();
String description = descriptionEt.getText().toString();
int user_id;
userdetails = sessionManager.getUserDetails();
User user = userdetails.get(0);
user_id = user.getUserid();
upload(title, "genre", description, songPath, user_id);
return null;
}
由于泄漏的Window,我在AsyncTask类的onPreExecute方法的最后一行中得到错误,这意味着我在执行doInBackground()方法时遇到错误。