protected final String invoke_delete_string(String url) throws IOException { DefaultHttpClient dhc = new DefaultHttpClient(); try { set_basic_auth(dhc); Log.i(_module_name, "DELETE: " + url); HttpDelete delete = new HttpDelete(url); sign_auth(delete); HttpResponse httpResponse = dhc.execute(delete); int statusCode = httpResponse.getStatusLine().getStatusCode(); String res = UHttpComponents.loadStringFromHttpResponse(httpResponse); if (!is_ok_status(statusCode)) { String phrase = httpResponse.getStatusLine().getReasonPhrase(); String msg = String.format("DELETE*STATUS: [%s:%s] %s <= %s", statusCode, phrase, res, url); throw new HttpIOException(msg, delete, statusCode, phrase, res); } Log.i( _module_name, String.format("DELETE-OK: %s <= %s", make_log_message(res.toString()), url)); return res; } catch (IOException e) { Log.w(_module_name, String.format("DELETE*IO: %s <= %s", e.getMessage(), url), e); throw e; } catch (Throwable e) { Log.w(_module_name, String.format("DELETE*Throwable: %s <= %s", e.getMessage(), url), e); throw new InvocationTargetIOException(e); } finally { dhc.getConnectionManager().shutdown(); } }
protected final String invoke_put_string(String url, Bundle params) throws IOException { DefaultHttpClient dhc = new DefaultHttpClient(); try { set_basic_auth(dhc); Log.i(_module_name, "PUT: " + url); HttpPut put = new HttpPut(url); sign_auth(put); if (params != null) { ArrayList<NameValuePair> nvps = new ArrayList<NameValuePair>(); for (String key : params.keySet()) { nvps.add(new BasicNameValuePair(key, params.getString(key))); } UrlEncodedFormEntity reqEntity = new UrlEncodedFormEntity(nvps, "UTF-8"); put.setEntity(reqEntity); } HttpResponse httpResponse = dhc.execute(put); int statusCode = httpResponse.getStatusLine().getStatusCode(); String res = UHttpComponents.loadStringFromHttpResponse(httpResponse); if (!is_ok_status(statusCode)) { String phrase = httpResponse.getStatusLine().getReasonPhrase(); String msg = String.format("PUT*STATUS: [%s:%s] %s <= %s", statusCode, phrase, res, url); throw new HttpIOException(msg, put, statusCode, phrase, res); } Log.i(_module_name, String.format("PUT-OK: %s <= %s", make_log_message(res.toString()), url)); return res; } catch (IOException e) { Log.w(_module_name, String.format("PUT*IO: %s <= %s", e.getMessage(), url), e); throw e; } catch (Throwable e) { Log.w(_module_name, String.format("PUT*Throwable: %s <= %s", e.getMessage(), url), e); throw new InvocationTargetIOException(e); } finally { dhc.getConnectionManager().shutdown(); } }
protected final JSONObject invoke_get_json(String url) throws IOException { DefaultHttpClient dhc = new DefaultHttpClient(); try { set_basic_auth(dhc); Log.i(_module_name, "GET: " + url); HttpGet get = new HttpGet(url); sign_auth(get); HttpResponse httpResponse = dhc.execute(get); int statusCode = httpResponse.getStatusLine().getStatusCode(); String res = UHttpComponents.loadStringFromHttpResponse(httpResponse); if (!is_ok_status(statusCode)) { String phrase = httpResponse.getStatusLine().getReasonPhrase(); String msg = String.format("GET*STATUS: [%s:%s] %s <= %s", statusCode, phrase, res, url); throw new HttpIOException(msg, get, statusCode, phrase, res); } Log.i(_module_name, String.format("GET-OK: %s <= %s", make_log_message(res), url)); return new JSONObject(res); } catch (JSONException e) { Log.w(_module_name, String.format("GET*JSON: %s <= %s", e.getMessage(), url), e); throw new IOException(e.getMessage()); } catch (IOException e) { Log.w(_module_name, String.format("GET*IO: %s <= %s", e.getMessage(), url), e); throw e; } catch (Throwable e) { Log.w(_module_name, String.format("GET*Throwable: %s <= %s", e.getMessage(), url), e); throw new InvocationTargetIOException(e); } finally { dhc.getConnectionManager().shutdown(); } }
protected final JSONObject invoke_put_json_with_upload( String url, LinkedHashMap<String, Object> params, LinkedHashMap<String, String> files) throws IOException { DefaultHttpClient dhc = new DefaultHttpClient(); try { set_basic_auth(dhc); HttpPut put = new HttpPut(url); sign_auth(put); MultipartEntity reqEntity = new MultipartEntity(); for (Map.Entry<String, Object> param : params.entrySet()) { reqEntity.addPart(param.getKey(), new StringBody(param.getValue().toString())); } for (Map.Entry<String, String> file : files.entrySet()) { FileBody bin = new FileBody(new File(file.getValue())); reqEntity.addPart(file.getKey(), bin); } put.setEntity(reqEntity); HttpResponse httpResponse = dhc.execute(put); int statusCode = httpResponse.getStatusLine().getStatusCode(); String res = UHttpComponents.loadStringFromHttpResponse(httpResponse); if (!is_ok_status(statusCode)) { String phrase = httpResponse.getStatusLine().getReasonPhrase(); String msg = String.format("PUT(form)*STATUS: [%s:%s] %s <= %s", statusCode, phrase, res, url); throw new HttpIOException(msg, put, statusCode, phrase, res); } Log.i( _module_name, String.format("PUT(form)-OK: %s <= %s", make_log_message(res.toString()), url)); return new JSONObject(res); } catch (JSONException e) { Log.w(_module_name, String.format("PUT(form)*JSON: %s <= %s", e.getMessage(), url), e); throw new IOException(e.getMessage()); } catch (IOException e) { Log.w(_module_name, String.format("PUT(form)*IO: %s <= %s", e.getMessage(), url), e); throw e; } catch (Throwable e) { Log.w(_module_name, String.format("PUT(form)*Throwable: %s <= %s", e.getMessage(), url), e); throw new InvocationTargetIOException(e); } finally { dhc.getConnectionManager().shutdown(); } }