【问题标题】:Managing multiple HTTP requests in Android在 Android 中管理多个 HTTP 请求
【发布时间】:2015-05-20 11:46:22
【问题描述】:

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

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

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

我还需要它是防崩溃的,如果我的应用崩溃了,我仍然会知道我在哪里停止并从那里继续,而不是从头开始。

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

【问题讨论】:

    标签: android asynchronous httprequest


    【解决方案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 以了解在完成后删除数据库上的哪个条目。

    @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();
    }
    

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

    【讨论】:

      猜你喜欢
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      • 2021-05-26
      • 1970-01-01
      相关资源
      最近更新 更多