use of org.apache.http.client.methods.HttpPost in project Anki-Android by Ramblurr.
the class HttpUtility method postReport.
public static Boolean postReport(String url, List<NameValuePair> values) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(values));
HttpResponse response = httpClient.execute(httpPost);
switch(response.getStatusLine().getStatusCode()) {
case 200:
Log.e(AnkiDroidApp.TAG, String.format("feedback report posted to %s", url));
return true;
default:
Log.e(AnkiDroidApp.TAG, String.format("feedback report posted to %s message", url));
Log.e(AnkiDroidApp.TAG, String.format("%d: %s", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
break;
}
} catch (ClientProtocolException ex) {
Log.e(AnkiDroidApp.TAG, ex.toString());
} catch (IOException ex) {
Log.e(AnkiDroidApp.TAG, ex.toString());
}
return false;
}
use of org.apache.http.client.methods.HttpPost in project Notes by MiCode.
the class GTaskClient method postRequest.
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
if (!mLoggedin) {
Log.e(TAG, "please login first");
throw new ActionFailureException("not logged in");
}
HttpPost httpPost = createHttpPost();
try {
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
list.add(new BasicNameValuePair("r", js.toString()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(entity);
// execute the post
HttpResponse response = mHttpClient.execute(httpPost);
String jsString = getResponseContent(response.getEntity());
return new JSONObject(jsString);
} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("postRequest failed");
} catch (IOException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("postRequest failed");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("unable to convert response content to jsonobject");
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("error occurs when posting request");
}
}
use of org.apache.http.client.methods.HttpPost in project PneumaticCraft by MineMaarten.
the class PastebinHandler method loginInternal.
public boolean loginInternal(String userName, String password) {
HttpPost httppost = new HttpPost("http://pastebin.com/api/api_login.php");
List<NameValuePair> params = new ArrayList<NameValuePair>(3);
params.add(new BasicNameValuePair("api_dev_key", DEV_KEY));
params.add(new BasicNameValuePair("api_user_name", userName));
params.add(new BasicNameValuePair("api_user_password", password));
try {
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
userKey = IOUtils.toString(instream, "UTF-8");
if (userKey.startsWith("Bad API request")) {
Log.warning("User tried to log in into pastebin, it responded with the following: " + userKey);
userKey = null;
return false;
}
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
use of org.apache.http.client.methods.HttpPost in project 9GAG by Mixiaoxiao.
the class MxxHttpUtil method doHttpPost.
/**
* Op Http post request , "404error" response if failed
*
* @param url
* @param map
* Values to request
* @return
*/
public static String doHttpPost(String url, HashMap<String, String> map) {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
ConnManagerParams.setTimeout(client.getParams(), TIMEOUT);
HttpPost post = new HttpPost(url);
post.setHeaders(headers);
String result = "ERROR";
ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
if (map != null) {
for (Map.Entry<String, String> entry : map.entrySet()) {
BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
pairList.add(pair);
}
}
try {
HttpEntity entity = new UrlEncodedFormEntity(pairList, "UTF-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity(), "UTF-8");
} else {
result = EntityUtils.toString(response.getEntity(), "UTF-8") + response.getStatusLine().getStatusCode() + "ERROR";
}
} catch (ConnectTimeoutException e) {
result = "TIMEOUTERROR";
} catch (Exception e) {
result = "OTHERERROR";
e.printStackTrace();
}
return result;
}
use of org.apache.http.client.methods.HttpPost in project nanohttpd by NanoHttpd.
the class GetAndPostIntegrationTest method testPostRequestWithEncodedParameters.
@Test
public void testPostRequestWithEncodedParameters() throws Exception {
this.testServer.response = "testPostRequestWithEncodedParameters";
HttpPost httppost = new HttpPost("http://localhost:8192/encodingtest");
HttpResponse response = this.httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
assertEquals("<html><head><title>Testé ça</title></head><body>Testé ça</body></html>", responseBody);
}
Aggregations