use of org.apache.http.client.entity.UrlEncodedFormEntity in project SmartAndroidSource by jaychou2012.
the class AbstractAjaxCallback method httpEntity.
private void httpEntity(String url, HttpEntityEnclosingRequestBase req, Map<String, String> headers, Map<String, Object> params, AjaxStatus status) throws ClientProtocolException, IOException {
//This setting seems to improve post performance
//http://stackoverflow.com/questions/3046424/http-post-requests-using-httpclient-take-2-seconds-why
req.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
HttpEntity entity = null;
Object value = params.get(AQuery.POST_ENTITY);
if (value instanceof HttpEntity) {
entity = (HttpEntity) value;
} else {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> e : params.entrySet()) {
value = e.getValue();
if (value != null) {
pairs.add(new BasicNameValuePair(e.getKey(), value.toString()));
}
}
entity = new UrlEncodedFormEntity(pairs, "UTF-8");
}
if (headers != null && !headers.containsKey("Content-Type")) {
headers.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
}
req.setEntity(entity);
httpDo(req, url, headers, status);
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project FastDev4Android by jiangqqlmj.
the class IoUtils method responseFromServiceByPostUTF.
/**
* post请求获取服务端数据 编码为UTF-8
*/
public static String responseFromServiceByPostUTF(String url, HashMap<String, String> map) {
if (url == null || url.equals("") || map == null) {
return null;
}
Log.d(TAG_LISTLOGIC, "post数据请求地址:" + url);
// 对密码进行加密处理
HttpPost httpPost = null;
URI encodedUri = null;
try {
encodedUri = new URI(url);
httpPost = new HttpPost(encodedUri);
} catch (URISyntaxException e) {
// 清理一些空格
String encodedUrl = url.replace(' ', '+');
httpPost = new HttpPost(encodedUrl);
e.printStackTrace();
}
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEF_CONN_TIMEOUT);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, DEF_SOCKET_TIMEOUT);
try {
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey().toString();
String value = null;
if (entry.getValue() == null) {
value = "";
} else {
value = entry.getValue().toString();
}
Log.d(TAG_LISTLOGIC, "post数据请求参数" + key + "=" + value);
BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, value);
nameValuePair.add(basicNameValuePair);
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse != null) {
int code = httpResponse.getStatusLine().getStatusCode();
if (code == HttpStatus.SC_OK) {
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity).trim();
Log.d(TAG_LISTLOGIC, "post数据请求服务器返回值200");
Log.d(TAG_LISTLOGIC, "post返回值:" + result);
return result;
} else {
httpPost.abort();
}
} else {
httpPost.abort();
}
} catch (Exception e) {
e.printStackTrace();
return null;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
} finally {
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
httpClient = null;
}
}
return null;
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project baker-android by bakerframework.
the class GCMRegistrationWorker method sendRegistrationIdToBackend.
private boolean sendRegistrationIdToBackend(String registrationId) {
boolean result = false;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(context.getString(R.string.post_apns_token_url));
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("app_id", context.getString(R.string.app_id)));
postParameters.add(new BasicNameValuePair("user_id", GindActivity.userAccount));
postParameters.add(new BasicNameValuePair("apns_token", registrationId));
postParameters.add(new BasicNameValuePair("device", "ANDROID"));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpClient.execute(httpPost);
if (null != response) {
if (response.getStatusLine().getStatusCode() == 200) {
result = true;
} else {
Log.e(this.getClass().toString(), "Device Registration ID failed, response from server was " + response.getStatusLine());
}
} else {
Log.e(this.getClass().toString(), "Device Registration ID failed, response is null");
}
} catch (Exception ex) {
Log.e(this.getClass().toString(), "Fatal error when trying to send the registration ID: " + ex.toString());
}
return result;
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project baker-android by bakerframework.
the class PostClient method doPost.
public String doPost() {
String result = "ERROR";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
if (this.parameters != null) {
httpPost.setEntity(new UrlEncodedFormEntity(this.parameters));
}
HttpResponse response = httpClient.execute(httpPost);
if (null != response) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
result = EntityUtils.toString(response.getEntity());
Log.d(this.getClass().toString(), "POST REQUEST SUCCEEDED: " + result);
} else {
Log.e(this.getClass().toString(), "BAD RESPONSE FROM SERVER: " + statusCode);
}
} else {
Log.e(this.getClass().toString(), "RESPONSE IS NULL");
}
} catch (Exception ex) {
Log.e(this.getClass().toString(), "EXCEPTION WHILE SENDING POST REQUEST: ", ex);
}
return result;
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project cw-andtutorials by commonsguy.
the class Patchy method updateStatus.
private void updateStatus() {
try {
String s = status.getText().toString();
HttpPost post = new HttpPost("https://identi.ca/api/statuses/update.json");
post.addHeader("Authorization", "Basic " + getCredentials());
List<NameValuePair> form = new ArrayList<NameValuePair>();
form.add(new BasicNameValuePair("status", s));
post.setEntity(new UrlEncodedFormEntity(form, HTTP.UTF_8));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(post, responseHandler);
JSONObject response = new JSONObject(responseBody);
} catch (Throwable t) {
Log.e("Patchy", "Exception in updateStatus()", t);
goBlooey(t);
}
}
Aggregations