use of org.apache.http.message.BasicNameValuePair in project Klyph by jonathangerbaud.
the class HttpRequest2 method send.
public String send() {
client = AndroidHttpClient.newInstance("Android");
HttpPost request = new HttpPost(url);
if (params != null) {
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
entity.setContentEncoding(HTTP.UTF_8);
request.setEntity(entity);
} catch (UnsupportedEncodingException e) {
Log.e("", "UnsupportedEncodingException: " + e);
}
}
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = "";
try {
response = client.execute(request, responseHandler);
} catch (ClientProtocolException e) {
Log.e("HttpRequest2", e.toString());
} catch (IOException e) {
Log.e("HttpRequest2", e.toString());
}
if (LOG_RESPONSE == true)
Log.i("HttpRequest2", response);
if (HTML_TRANSFORM == true)
response = Html.fromHtml(response).toString();
close();
return response;
}
use of org.apache.http.message.BasicNameValuePair in project FastDev4Android by jiangqqlmj.
the class IoUtils method sendMessageByPost.
/**
* post投递
*/
public static void sendMessageByPost(String url, HashMap<String, String> map) {
if (url == null || url.equals("")) {
return;
}
Log.d(TAG_LISTLOGIC, "post投递地址:" + url);
HttpPost httpPost = null;
URI encodedUri = getEncodingURI(url);
httpPost = new HttpPost(encodedUri);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DELIVER_CONN_TIMEOUT);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, DELIVER_SOCKET_TIMEOUT);
try {
if (map != null) {
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, HTTP.UTF_8));
}
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse != null) {
int code = httpResponse.getStatusLine().getStatusCode();
if (code == HttpStatus.SC_OK) {
Log.d(TAG_LISTLOGIC, "post投递服务器返回200");
return;
} else {
httpPost.abort();
}
} else {
httpPost.abort();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
httpClient = null;
}
}
}
use of org.apache.http.message.BasicNameValuePair in project SmartAndroidSource by jaychou2012.
the class RequestParams method toString.
@Override
public String toString() {
StringBuilder result = new StringBuilder();
for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
if (result.length() > 0)
result.append("&");
result.append(entry.getKey());
result.append("=");
result.append(entry.getValue());
}
for (ConcurrentHashMap.Entry<String, StreamWrapper> entry : streamParams.entrySet()) {
if (result.length() > 0)
result.append("&");
result.append(entry.getKey());
result.append("=");
result.append("STREAM");
}
for (ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
if (result.length() > 0)
result.append("&");
result.append(entry.getKey());
result.append("=");
result.append("FILE");
}
List<BasicNameValuePair> params = getParamsList(null, urlParamsWithObjects);
for (BasicNameValuePair kv : params) {
if (result.length() > 0)
result.append("&");
result.append(kv.getName());
result.append("=");
result.append(kv.getValue());
}
return result.toString();
}
use of org.apache.http.message.BasicNameValuePair in project SmartAndroidSource by jaychou2012.
the class RequestParams method getParamsList.
protected List<BasicNameValuePair> getParamsList() {
List<BasicNameValuePair> lparams = new LinkedList<BasicNameValuePair>();
for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
lparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
lparams.addAll(getParamsList(null, urlParamsWithObjects));
return lparams;
}
use of org.apache.http.message.BasicNameValuePair in project nanohttpd by NanoHttpd.
the class GetAndPostIntegrationTest method testPostRequestWithFormEncodedParameters.
@Test
public void testPostRequestWithFormEncodedParameters() throws Exception {
this.testServer.response = "testPostRequestWithFormEncodedParameters";
HttpPost httppost = new HttpPost("http://localhost:8192/");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("age", "120"));
postParameters.add(new BasicNameValuePair("gender", "Male"));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = this.httpclient.execute(httppost, responseHandler);
assertEquals("POST:testPostRequestWithFormEncodedParameters-params=2;age=120;gender=Male", responseBody);
}
Aggregations