在Android中管理多个HTTP请求

时间:2015-05-20 11:46:23

标签: android asynchronous httprequest

我想让我的应用程序获取手机上的所有媒体,然后将实际数据发送到我的服务器。

我在电话上获得了媒体的所有绝对路径,现在我想要管理请求。有没有办法制作某种请求数组,它会一直运行直到它为空?

如果可以,我可以将其升级为优先级数组吗?例如,有人上传了100MB的媒体,我希望他们继续浏览我的应用程序,如果他们采取行动,那个特定的操作将进入该请求数组轮询,但具有高优先级?好像我要发送的下一个请求是我刚创建的那个?

我还需要它作为防撞证明,如果我的应用程序崩溃,我仍然会知道我停在哪里并从那里继续而不是从头开始。

我知道这有很多要求,但我没有经验足以知道它是否可能。

1 个答案:

答案 0 :(得分:1)

就我的研究而言,这是我的解决方案。

创建一个名为myThread的可调用类,它将处理请求本身,并将删除和更改SQLite DB上请求的状态。

public class MyThread implements Callable {

private File _file;
private Context context;
private DBHelper helper;

public MyThread(File file, Context context) {
    this._file = file;
    this.context = context;
}

@Override
public String call() throws Exception {
    HttpClient client = Utility.getNewHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost post = new HttpPost("http://192.168.9.62/mobile_api/timeline/moment/upload");
    try {
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        FileBody fileBody = new FileBody(_file);
        builder.addPart("content", fileBody);
        builder.addPart("type", new StringBody("file", ContentType.TEXT_PLAIN));
        builder.addPart("title", new StringBody("service test", ContentType.TEXT_PLAIN));
        builder.addPart("userType", new StringBody("user", ContentType.TEXT_PLAIN));
        builder.addPart("uid", new StringBody(MyInfiActivity.friends_uid, ContentType.TEXT_PLAIN));
        builder.addPart("momentId", new StringBody("1", ContentType.TEXT_PLAIN));
        builder.addPart("storyId", new StringBody("8", ContentType.TEXT_PLAIN));
        Utility.addCookiesToPost(post);

        post.setEntity(builder.build());
        client.execute(post, localContext);
    } catch (IOException e) {
        Log.e("Callable try", post.toString());

    }
    return "1";
}

IntentService创建5个线程(为了更好的性能...不确定它有多好)每个线程获得MyThread可调用,以便在完成时知道要在DB上删除哪个条目。

@Override
protected void onHandleIntent(Intent intent) {
    helper = new DBHelper(getApplicationContext());
    executor = Executors.newFixedThreadPool(5);
    File file;
    Log.e("requestsExists",helper.requestsExists()+"");
    while(helper.requestsExists()){
        ArrayList<String> requestArr = helper.getRequestsToExcute(5);
        //checks if the DB requests exists
        if(!requestArr.isEmpty()){
            //execute them and delete the DB entry
            for(int i=0;i<requestArr.size();i++){
                file = new File(requestArr.get(i));

                Log.e("file",file.toString());
                Future<String> future = executor.submit(new MyThread(file,getApplicationContext()));

                Log.e("future object", future.toString());
                try {
                    long idToDelete = Long.parseLong(future.get());
                    Log.e("THREAD ANSWER", future.get() + "");
                    helper.deleteRequest(idToDelete);
                } catch (InterruptedException e) {
                    Log.e("future try", "");
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    executor.shutdown();
}

注意:数据是硬编码用于测试,将根据我将发送的媒体包含变量

相关问题