我使用异步任务在服务器上传图像,最后我想返回上传文件的值。我怎样才能做到这一点 我叫asynctask为
new Config.UploadFileToServer(loginUserInfoId, uploadedFileURL).execute();
和我的asynctask函数如下:
public static final class UploadFileToServer extends AsyncTask<Void, Integer, String> {
String loginUserInfoId = "";
String filePath = "";
long totalSize = 0;
public UploadFileToServer(String userInfoId, String url){
loginUserInfoId = userInfoId;
filePath = url;
}
@Override
protected void onPreExecute() {
// setting progress bar to zero
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
// Making progress bar visible
// updating progress bar value
}
@Override
protected String doInBackground(Void... params) {
return uploadFile();
}
@SuppressWarnings("deprecation")
private String uploadFile() {
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Config.HOST_NAME + "/AndroidApp/AddMessageFile/"+loginUserInfoId);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new AndroidMultiPartEntity.ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(filePath);
// Adding file data to http body
entity.addPart("file", new FileBody(sourceFile));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
responseString = responseString.replace("\"","");
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
答案 0 :(得分:0)
尝试下面给出的代码。
public Result CallServer(String params)
{
try
{
MainAynscTask task = new MainAynscTask();
task.execute(params);
Result aResultM = task.get(); //Add this
}
catch(Exception ex)
{
ex.printStackTrace();
}
return aResultM;//Need to get back the result
}
答案 1 :(得分:0)
你差不多了,你应该只做一步。正如我所看到的,您将使用doInBackground
方法返回结果(作为调用uploadFile
的结果)。现在,此值将传递给onPostExecute
方法,该方法在主线程上执行。在其正文中,您应该通知正在等待结果的组件,结果到达。有很多方法可以做到,但是如果你不想使用第三方库,最简单的方法应该是在AsyncTask
构造函数中注入监听器并在onPostExecute
调用它。例如,您可以声明以下界面:
public interface MyListener {
void onDataArrived(String data);
}
并在AsyncTask
构造函数中注入实现它的实例:
public UploadFileToServer(String userInfoId, String url, MyListener listener){
loginUserInfoId = userInfoId;
filePath = url;
mListener = listener;
}
现在,您可以在onPostExecute
:
@Override
protected void onPostExecute(String result) {
listener.onDataArrived(result);
super.onPostExecute(result); //actually `onPostExecute` in base class does nothing, so this line can be removed safely
}
如果您正在寻找更复杂的解决方案,可以从阅读this文章开始。