コード例 #1
0
 @Override
 protected String doInBackground(LocalIndexInfo... params) {
   int count = 0;
   int total = 0;
   for (LocalIndexInfo info : params) {
     if (!isCancelled()) {
       String warning = null;
       try {
         File file = new File(info.getPathToData());
         String userName = settings.USER_NAME.get();
         String pwd = settings.USER_PASSWORD.get();
         String url =
             URL_TO_UPLOAD_GPX
                 + "?author="
                 + URLEncoder.encode(userName, "UTF-8")
                 + "&wd="
                 + URLEncoder.encode(pwd, "UTF-8")
                 + "&file="
                 + URLEncoder.encode(file.getName(), "UTF-8");
         warning = Algoritms.uploadFile(url, file, "filename", true);
       } catch (UnsupportedEncodingException e) {
         warning = e.getMessage();
       }
       total++;
       if (warning == null) {
         count++;
       } else {
         publishProgress(warning);
       }
     }
   }
   return getString(R.string.local_index_items_uploaded, count, total);
 }
コード例 #2
0
 public String uploadGPXFile(String tagstring, String description, String visibility, File f) {
   String url = URL_TO_UPLOAD_GPX;
   Map<String, String> additionalData = new LinkedHashMap<String, String>();
   additionalData.put("description", description);
   additionalData.put("tags", tagstring);
   additionalData.put("visibility", visibility);
   return NetworkUtils.uploadFile(
       url,
       f,
       settings.USER_NAME.get() + ":" + settings.USER_PASSWORD.get(),
       "file",
       true,
       additionalData);
 }
コード例 #3
0
  private String sendRequest(
      String url,
      String requestMethod,
      String requestBody,
      String userOperation,
      boolean doAuthenticate) {
    log.info("Sending request " + url); // $NON-NLS-1$
    //		if(true){
    //			return sendRequsetThroughHttpClient(url, requestMethod, requestBody, userOperation,
    // doAuthenticate);
    //		}

    try {
      HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();

      connection.setConnectTimeout(15000);
      connection.setRequestMethod(requestMethod);
      StringBuilder responseBody = new StringBuilder();
      if (doAuthenticate) {
        String token = settings.USER_NAME.get() + ":" + settings.USER_PASSWORD.get(); // $NON-NLS-1$
        connection.addRequestProperty(
            "Authorization",
            "Basic "
                + Base64.encode(
                    token.getBytes("UTF-8"))); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      }
      connection.setDoInput(true);
      if (requestMethod.equals("PUT")
          || requestMethod.equals("POST")
          || requestMethod.equals("DELETE")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-type", "text/xml"); // $NON-NLS-1$ //$NON-NLS-2$
        OutputStream out = connection.getOutputStream();
        if (requestBody != null) {
          BufferedWriter bwr =
              new BufferedWriter(new OutputStreamWriter(out, "UTF-8"), 1024); // $NON-NLS-1$
          bwr.write(requestBody);
          bwr.flush();
        }
        out.close();
      }
      connection.connect();
      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        String msg =
            userOperation
                + " "
                + ctx.getString(R.string.failed_op)
                + " : "
                + connection.getResponseMessage(); // $NON-NLS-1$//$NON-NLS-2$
        log.error(msg);
        showWarning(msg);
      } else {
        log.info("Response : " + connection.getResponseMessage()); // $NON-NLS-1$
        // populate return fields.
        responseBody.setLength(0);
        InputStream i = connection.getInputStream();
        if (i != null) {
          BufferedReader in =
              new BufferedReader(new InputStreamReader(i, "UTF-8"), 256); // $NON-NLS-1$
          String s;
          boolean f = true;
          while ((s = in.readLine()) != null) {
            if (!f) {
              responseBody.append("\n"); // $NON-NLS-1$
            } else {
              f = false;
            }
            responseBody.append(s);
          }
        }
        return responseBody.toString();
      }
    } catch (NullPointerException e) {
      // that's tricky case why NPE is thrown to fix that problem httpClient could be used
      String msg = ctx.getString(R.string.auth_failed);
      log.error(msg, e);
      showWarning(msg);
    } catch (MalformedURLException e) {
      log.error(userOperation + " " + ctx.getString(R.string.failed_op), e); // $NON-NLS-1$
      showWarning(
          MessageFormat.format(
              ctx.getResources().getString(R.string.poi_error_unexpected_template), userOperation));
    } catch (IOException e) {
      log.error(userOperation + " " + ctx.getString(R.string.failed_op), e); // $NON-NLS-1$
      showWarning(
          MessageFormat.format(
              ctx.getResources().getString(R.string.poi_error_io_error_template), userOperation));
    }

    return null;
  }
コード例 #4
0
  protected String sendRequsetThroughHttpClient(
      String url,
      String requestMethod,
      String requestBody,
      String userOperation,
      boolean doAuthenticate) {
    StringBuilder responseBody = new StringBuilder();
    try {

      HttpParams params = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(params, 15000);
      DefaultHttpClient httpclient = new DefaultHttpClient(params);
      if (doAuthenticate) {
        UsernamePasswordCredentials credentials =
            new UsernamePasswordCredentials(
                settings.USER_NAME.get()
                    + ":" //$NON-NLS-1$
                    + settings.USER_PASSWORD.get());
        httpclient
            .getCredentialsProvider()
            .setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), credentials);
      }
      HttpRequestBase method = null;
      if (requestMethod.equals("GET")) { // $NON-NLS-1$
        method = new HttpGet(url);
      } else if (requestMethod.equals("POST")) { // $NON-NLS-1$
        method = new HttpPost(url);
      } else if (requestMethod.equals("PUT")) { // $NON-NLS-1$
        method = new HttpPut(url);
      } else if (requestMethod.equals("DELETE")) { // $NON-NLS-1$
        method = new HttpDelete(url);

      } else {
        throw new IllegalArgumentException(requestMethod + " is invalid method"); // $NON-NLS-1$
      }
      if (requestMethod.equals("PUT")
          || requestMethod.equals("POST")
          || requestMethod.equals("DELETE")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        // TODO add when needed
        //				connection.setDoOutput(true);
        //				connection.setRequestProperty("Content-type", "text/xml");
        //				OutputStream out = connection.getOutputStream();
        //				if (requestBody != null) {
        //					BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
        //					bwr.write(requestBody);
        //					bwr.flush();
        //				}
        //				out.close();
      }

      HttpResponse response = httpclient.execute(method);
      if (response.getStatusLine() == null || response.getStatusLine().getStatusCode() != 200) {

        String msg;
        if (response.getStatusLine() != null) {
          msg = userOperation + " " + ctx.getString(R.string.failed_op); // $NON-NLS-1$
        } else {
          msg =
              userOperation
                  + " "
                  + ctx.getString(R.string.failed_op)
                  + response.getStatusLine().getStatusCode()
                  + " : "
                  + //$NON-NLS-1$//$NON-NLS-2$
                  response.getStatusLine().getReasonPhrase();
        }
        log.error(msg);
        showWarning(msg);
      } else {
        InputStream is = response.getEntity().getContent();
        if (is != null) {
          BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); // $NON-NLS-1$
          String s;
          while ((s = in.readLine()) != null) {
            responseBody.append(s);
            responseBody.append("\n"); // $NON-NLS-1$
          }
          is.close();
        }
        httpclient.getConnectionManager().shutdown();
        return responseBody.toString();
      }
    } catch (MalformedURLException e) {
      log.error(userOperation + " failed", e); // $NON-NLS-1$
      showWarning(
          MessageFormat.format(
              ctx.getResources().getString(R.string.poi_error_unexpected_template), userOperation));
    } catch (IOException e) {
      log.error(userOperation + " failed", e); // $NON-NLS-1$
      showWarning(
          MessageFormat.format(
              ctx.getResources().getString(R.string.poi_error_unexpected_template), userOperation));
    }
    return null;
  }