Search in sources :

Example 41 with BasicNameValuePair

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Map(java.util.Map)

Example 42 with BasicNameValuePair

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;
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Map(java.util.Map)

Example 43 with BasicNameValuePair

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();
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 44 with BasicNameValuePair

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;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedList(java.util.LinkedList)

Example 45 with BasicNameValuePair

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);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Test(org.junit.Test)

Aggregations

BasicNameValuePair (org.apache.http.message.BasicNameValuePair)289 NameValuePair (org.apache.http.NameValuePair)199 ArrayList (java.util.ArrayList)187 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)101 HttpPost (org.apache.http.client.methods.HttpPost)87 HttpResponse (org.apache.http.HttpResponse)77 HttpEntity (org.apache.http.HttpEntity)58 IOException (java.io.IOException)55 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)33 Test (org.junit.Test)32 HttpGet (org.apache.http.client.methods.HttpGet)29 ClientProtocolException (org.apache.http.client.ClientProtocolException)28 CSVReader (com.talend.csv.CSVReader)27 HttpClient (org.apache.http.client.HttpClient)25 UnsupportedEncodingException (java.io.UnsupportedEncodingException)23 HashMap (java.util.HashMap)20 JSONObject (org.json.JSONObject)20 Map (java.util.Map)19 URI (java.net.URI)16 WebserviceInvocation (com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation)15