Search in sources :

Example 76 with UrlEncodedFormEntity

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);
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpEntity(org.apache.http.HttpEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Map(java.util.Map) HashMap(java.util.HashMap)

Example 77 with UrlEncodedFormEntity

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) 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 78 with UrlEncodedFormEntity

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;
}
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) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 79 with UrlEncodedFormEntity

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 80 with UrlEncodedFormEntity

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

Aggregations

UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)134 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)111 HttpPost (org.apache.http.client.methods.HttpPost)105 NameValuePair (org.apache.http.NameValuePair)100 ArrayList (java.util.ArrayList)96 HttpResponse (org.apache.http.HttpResponse)74 IOException (java.io.IOException)47 HttpEntity (org.apache.http.HttpEntity)40 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)31 ClientProtocolException (org.apache.http.client.ClientProtocolException)27 UnsupportedEncodingException (java.io.UnsupportedEncodingException)26 HttpClient (org.apache.http.client.HttpClient)20 Test (org.junit.Test)20 HttpGet (org.apache.http.client.methods.HttpGet)19 Map (java.util.Map)18 JSONObject (org.json.JSONObject)18 TestHttpClient (io.undertow.testutils.TestHttpClient)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 HashMap (java.util.HashMap)13 Header (org.apache.http.Header)13