예제 #1
0
  private String doGet(String url, Map<String, String> param) throws WeiboException {

    HttpGet httpGet = new HttpGet();
    URIBuilder uriBuilder;
    try {
      uriBuilder = new URIBuilder(url);

      Set<String> keys = param.keySet();

      for (String key : keys) {
        String value = param.get(key);
        if (!TextUtils.isEmpty(value)) uriBuilder.addParameter(key, param.get(key));
      }

      httpGet.setURI(uriBuilder.build());

      AppLogger.d(uriBuilder.build().toString());

    } catch (URISyntaxException e) {
      AppLogger.d(e.getMessage());
    }

    CookieStore cookieStore = new BasicCookieStore();

    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpResponse response = getHttpResponse(httpGet, localContext);

    if (response != null) {
      return dealWithResponse(response);
    } else {
      return "";
    }
  }
예제 #2
0
  /**
   * Scenario: Pretend a partially downloaded file already exists.
   *
   * <p>Verify that: * Range header is set in request * Content will be appended to existing file *
   * Content will be marked as downloaded in catalog
   */
  @Test
  public void testResumingDownloadFromExistingFile() throws Exception {
    DownloadContent content =
        new DownloadContent.Builder()
            .setKind(DownloadContent.KIND_FONT)
            .setType(DownloadContent.TYPE_ASSET_ARCHIVE)
            .setSize(4223)
            .build();

    DownloadContentCatalog catalog = mock(DownloadContentCatalog.class);
    doReturn(Collections.singletonList(content)).when(catalog).getScheduledDownloads();

    DownloadAction action = spy(new DownloadAction(null));
    doReturn(false).when(action).isActiveNetworkMetered(RuntimeEnvironment.application);

    File temporaryFile = mockFileWithSize(1337L);
    doReturn(temporaryFile)
        .when(action)
        .createTemporaryFile(RuntimeEnvironment.application, content);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    doReturn(outputStream).when(action).openFile(eq(temporaryFile), anyBoolean());

    HttpClient client = mockHttpClient(HttpStatus.SC_PARTIAL_CONTENT, "HelloWorld");
    doReturn(client).when(action).buildHttpClient();

    File destinationFile = mockNotExistingFile();
    doReturn(destinationFile)
        .when(action)
        .getDestinationFile(RuntimeEnvironment.application, content);

    doReturn(true).when(action).verify(eq(temporaryFile), anyString());
    doNothing().when(action).extract(eq(temporaryFile), eq(destinationFile), anyString());

    action.perform(RuntimeEnvironment.application, catalog);

    ArgumentCaptor<HttpGet> argument = ArgumentCaptor.forClass(HttpGet.class);
    verify(client).execute(argument.capture());

    HttpGet request = argument.getValue();
    Assert.assertTrue(request.containsHeader("Range"));
    Assert.assertEquals("bytes=1337-", request.getFirstHeader("Range").getValue());
    Assert.assertEquals("HelloWorld", new String(outputStream.toByteArray(), "UTF-8"));

    verify(action).openFile(eq(temporaryFile), eq(true));
    verify(catalog).markAsDownloaded(content);
    verify(temporaryFile).delete();
  }
예제 #3
0
  /** don't need error message to show */
  private String doGetSaveFile(
      String url, String path, FileDownloaderHttpHelper.DownloadListener downloadListener) {

    URIBuilder uriBuilder;
    HttpGet httpGet = new HttpGet();
    try {
      uriBuilder = new URIBuilder(url);

      httpGet.setURI(uriBuilder.build());

      AppLogger.d(uriBuilder.build().toString());

    } catch (URISyntaxException e) {
      AppLogger.d(e.getMessage());
    }

    HttpResponse response = null;
    try {

      response = httpClient.execute(httpGet);

    } catch (ConnectTimeoutException ignored) {

      AppLogger.e(ignored.getMessage());

    } catch (ClientProtocolException ignored) {
      AppLogger.e(ignored.getMessage());

    } catch (IOException ignored) {
      AppLogger.e(ignored.getMessage());
    }

    if (response != null) {

      return FileDownloaderHttpHelper.saveFile(response, path, downloadListener);

    } else {
      return "";
    }
  }