Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 向服务器发送映像时异步不工作?_Android_Android Asynctask - Fatal编程技术网

Android 向服务器发送映像时异步不工作?

Android 向服务器发送映像时异步不工作?,android,android-asynctask,Android,Android Asynctask,我需要将图像发送到服务,预执行方法在后台不工作的地方工作。代码如下所示: private class SendImageAsync extends AsyncTask<String, String, String> { private Context context; private String serverUrl; private String path; public SendImageAsync(Con

我需要将图像发送到服务,预执行方法在后台不工作的地方工作。代码如下所示:

    private class SendImageAsync extends AsyncTask<String, String, String> {
        private Context context;
        private String serverUrl;
        private String path;

        public SendImageAsync(Context context, String serverUrl,
                 String path) {
            this.context = context;
            this.serverUrl = serverUrl;
            this.path = path;
        }

        @Override
        protected void onPreExecute() {
            LogWrite.i(TAG, "onPreExecute method enters!!");

            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            LogWrite.i(TAG, "doInBackground method enters!!");
            byte[] imageBytes = getFileBytes(path);
            ImageToSend sendImage = new ImageToSend();
            boolean responseStatus = sendImage.send(context, serverUrl,
                    imageBytes );
            String responseString = responseStatus ? "Success!" : "Fail!";
            LogWrite.e(TAG, "Response " + responseString);
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            LogWrite.i(TAG, "onPostExecute method enters!!");
            super.onPostExecute(result);
        }
    }

我不确定,但您可能希望发送请求而不是响应

我不确定,但您可能希望发送请求而不是响应

我找到了解决方案,我在另一个AsyncTask上使用了这个AsyncTask,所以它不起作用,甚至没有出现任何异常。

我找到了解决方案,我在另一个AsyncTask上使用了这个AsyncTask,所以它不起作用,甚至没有出现任何异常。

以下是您可以参考的基本示例代码,希望对您有所帮助

/**
 * Async task to post file to remote web api
 */
public class PostFileToWebAPI extends AsyncTask<String, Integer, Object> {    

   ...

    @Override
    protected Object doInBackground(String... params) {
        String filePath = params[1];
        try {                               
            return postFile(mAPI_URL, filePath);
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);            
    }      
}


private String postFile(String apiUrl, String filePath) throws Exception {
    // init httpClient, httpPost...
    ...

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    File file = new File(filePath);
    FileBody fileBody = new FileBody(file);
    final long totalSize = fileBody.getContentLength();
    builder.addPart("file", fileBody);        
    httpEntity = builder.build();      
    httpPost.setEntity(httpEntity);
    httpResponse = httpClient.execute(httpPost);
    statusCode = httpResponse.getStatusLine().getStatusCode();

    if ((statusCode != HttpStatus.SC_OK) && (statusCode != HttpStatus.SC_INTERNAL_SERVER_ERROR)) {
        return httpResponse.getStatusLine().toString();
    } else {
        return getStringContent(httpResponse);
    }
}
/**
*将文件发布到远程web api的异步任务
*/
公共类PostFileToWebAPI扩展异步任务{
...
@凌驾
受保护对象doInBackground(字符串…参数){
字符串filePath=params[1];
试试{
返回postFile(mAPI_URL、文件路径);
}捕获(例外e){
返回e.getMessage();
}
}
@凌驾
受保护的void onPostExecute(对象o){
super.onPostExecute(o);
}      
}
私有字符串postFile(字符串apirl、字符串filePath)引发异常{
//初始化httpClient,httpPost。。。
...
MultipartEntityBuilder=MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_兼容);
文件文件=新文件(文件路径);
FileBody FileBody=新的FileBody(文件);
final long totalSize=fileBody.getContentLength();
builder.addPart(“文件”,文件体);
httpEntity=builder.build();
httpPost.setEntity(httpEntity);
httpResponse=httpClient.execute(httpPost);
statusCode=httpResponse.getStatusLine().getStatusCode();
if((statusCode!=HttpStatus.SC_确定)和&(statusCode!=HttpStatus.SC_内部_服务器_错误)){
返回httpResponse.getStatusLine().toString();
}否则{
返回getStringContent(httpResponse);
}
}

以下是您可以参考的基本示例代码,希望对您有所帮助

/**
 * Async task to post file to remote web api
 */
public class PostFileToWebAPI extends AsyncTask<String, Integer, Object> {    

   ...

    @Override
    protected Object doInBackground(String... params) {
        String filePath = params[1];
        try {                               
            return postFile(mAPI_URL, filePath);
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);            
    }      
}


private String postFile(String apiUrl, String filePath) throws Exception {
    // init httpClient, httpPost...
    ...

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    File file = new File(filePath);
    FileBody fileBody = new FileBody(file);
    final long totalSize = fileBody.getContentLength();
    builder.addPart("file", fileBody);        
    httpEntity = builder.build();      
    httpPost.setEntity(httpEntity);
    httpResponse = httpClient.execute(httpPost);
    statusCode = httpResponse.getStatusLine().getStatusCode();

    if ((statusCode != HttpStatus.SC_OK) && (statusCode != HttpStatus.SC_INTERNAL_SERVER_ERROR)) {
        return httpResponse.getStatusLine().toString();
    } else {
        return getStringContent(httpResponse);
    }
}
/**
*将文件发布到远程web api的异步任务
*/
公共类PostFileToWebAPI扩展异步任务{
...
@凌驾
受保护对象doInBackground(字符串…参数){
字符串filePath=params[1];
试试{
返回postFile(mAPI_URL、文件路径);
}捕获(例外e){
返回e.getMessage();
}
}
@凌驾
受保护的void onPostExecute(对象o){
super.onPostExecute(o);
}      
}
私有字符串postFile(字符串apirl、字符串filePath)引发异常{
//初始化httpClient,httpPost。。。
...
MultipartEntityBuilder=MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_兼容);
文件文件=新文件(文件路径);
FileBody FileBody=新的FileBody(文件);
final long totalSize=fileBody.getContentLength();
builder.addPart(“文件”,文件体);
httpEntity=builder.build();
httpPost.setEntity(httpEntity);
httpResponse=httpClient.execute(httpPost);
statusCode=httpResponse.getStatusLine().getStatusCode();
if((statusCode!=HttpStatus.SC_确定)和&(statusCode!=HttpStatus.SC_内部_服务器_错误)){
返回httpResponse.getStatusLine().toString();
}否则{
返回getStringContent(httpResponse);
}
}

请完整、准确地解释“不工作”的含义。预执行方法日志显示了后台日志中的位置,但没有显示为什么在不使用任何参数的情况下声明asynctask,最好将其定义为
asynctask
我这样做是为了检查async不工作的原因,但是没有发现任何问题..请完整准确地解释“不工作”的含义。预执行方法日志显示了在后台日志中的位置,但没有显示为什么您在不使用任何参数的情况下这样声明asynctask更好地将其定义为
asynctask
我这样做是为了检查async不工作的原因,但是没有发现任何问题。因此,请尝试将此任务放在另一个任务的onPostExecute中。因此,请尝试将此任务放在另一个任务的onPostExecute中。