Example usage for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity

List of usage examples for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity

Introduction

In this page you can find the example usage for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity.

Prototype

public UrlEncodedFormEntity(final Iterable<? extends NameValuePair> parameters) 

Source Link

Document

Constructs a new UrlEncodedFormEntity with the list of parameters with the default encoding of HTTP#DEFAULT_CONTENT_CHARSET

Usage

From source file:thiru.in.basicauthwebview.Authenticate.java

@Override
protected CookieStore doInBackground(String... data) {
    String url = data[0];/*from www .  java 2  s .c  o  m*/
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("token", data[1]));
    DefaultHttpClient httpClient = new DefaultHttpClient();

    BasicCookieStore cookieStore = new BasicCookieStore();
    httpClient.setCookieStore(cookieStore);

    HttpPost httpPost = new HttpPost(url);
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            List<Cookie> cookies = cookieStore.getCookies();

            for (Cookie cookie : cookies) {
                Log.i("Cookie", cookie.getName() + " ==> " + cookie.getValue());
            }
        } else {
            Log.i("Authenticate", "Invalid user and password combination");
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return cookieStore;
}

From source file:com.allegrorom.ota.GCMIntentService.java

@Override
protected void onRegistered(Context ctx, String regID) {
    Log.v("OTA::GCMRegister", "GCM registered - ID=" + regID);
    ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("do", "register"));
    params.add(new BasicNameValuePair("reg_id", regID));
    params.add(new BasicNameValuePair("device", android.os.Build.DEVICE.toLowerCase()));
    params.add(new BasicNameValuePair("rom_id", Utils.getRomID()));
    params.add(new BasicNameValuePair("device_id",
            ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId()));

    try {//from ww  w  . j  a v  a 2 s.  co m
        HttpClient http = new DefaultHttpClient();
        HttpPost req = new HttpPost(Config.GCM_REGISTER_URL);
        req.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse r = http.execute(req);
        int status = r.getStatusLine().getStatusCode();
        HttpEntity e = r.getEntity();
        if (status == 200) {
            String data = EntityUtils.toString(e);
            if (data.length() == 0) {
                Log.w("OTA::GCMRegister", "No response to registration");
                return;
            }
            JSONObject json = new JSONObject(data);

            if (json.length() == 0) {
                Log.w("OTA::GCMRegister", "Empty response to registration");
                return;
            }

            if (json.has("error")) {
                Log.e("OTA::GCMRegister", json.getString("error"));
                return;
            }

            RomInfo info = new RomInfo(json.getString("rom"), json.getString("version"),
                    json.getString("changelog"), json.getString("url"), json.getString("md5"),
                    Utils.parseDate(json.getString("date")));

            final Config cfg = Config.getInstance(getApplicationContext());
            if (Utils.isUpdate(info)) {
                cfg.storeUpdate(info);
                if (cfg.getShowNotif()) {
                    Utils.showUpdateNotif(getApplicationContext(), info);
                } else {
                    Log.v("OTA::GCMRegister", "got update response, notif not shown");
                }
            } else {
                cfg.clearStoredUpdate();
            }
        } else {
            if (e != null)
                e.consumeContent();
            Log.w("OTA::GCMRegister", "registration response " + status);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.wso2.developerstudio.appcloud.utils.client.HttpsJaggeryClient.java

public static String httpPostLogin(String postURL, Map<String, String> requestParams) {

    httpClient = new DefaultHttpClient();
    httpClient = HttpsJaggeryClient.wrapClient(httpClient, postURL);
    HttpPost postRequest = new HttpPost(postURL);
    String jsonResponse = "";
    HttpResponse postResponse = null;//from w  w  w .  j  av  a 2  s.  com

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        Set<String> paramKeys = requestParams.keySet();

        for (String paramKey : paramKeys) {
            nameValuePairs.add(new BasicNameValuePair(paramKey, requestParams.get(paramKey)));
        }
        postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        postResponse = httpClient.execute(postRequest);

        if (STATUS_OK == postResponse.getStatusLine().getStatusCode()) {
            sessionCookie = postResponse.getFirstHeader(SET_COOKIE_HEADER).getValue().split(";")[0];
            HttpEntity entity = postResponse.getEntity();
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
            StringBuilder responseBuilder = new StringBuilder();
            String line = "";
            while ((line = reader.readLine()) != null) {
                responseBuilder.append(line);
            }
            jsonResponse = responseBuilder.toString();
            if (FALSE_RESPONSE.equals(jsonResponse)) {
                Authenticator.getInstance().setErrorcode(ErrorType.INVALID);
            }
            EntityUtils.consume(entity);
            if (entity != null) {
                entity.getContent().close();
            }
        } else {
            Authenticator.getInstance().setErrorcode(ErrorType.FAILD);
            Authenticator.getInstance().setErrormsg(postResponse.getStatusLine().getReasonPhrase());
            log.error("(" + postResponse.getStatusLine().getStatusCode() + ")" + ":"
                    + postResponse.getStatusLine().getReasonPhrase());
            return FALSE_RESPONSE;
        }
    } catch (Exception e) {
        Authenticator.getInstance().setErrorcode(ErrorType.ERROR);
        log.error("Connection failure", e);
        return FALSE_RESPONSE;
    } finally {
        httpClient.getConnectionManager().closeExpiredConnections();
    }
    return jsonResponse;
}

From source file:com.memetro.android.oauth.OAuth.java

public JSONObject call(String controller, String action, Map<String, String> params) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(oauthServer + controller + "/" + action);

    try {/*w w  w .j  a  va 2 s.  c om*/
        // Add data
        httppost.setEntity(new UrlEncodedFormEntity(Map2NameValuePair(params)));

        // Execute Post
        HttpResponse response = httpclient.execute(httppost);

        // Catch headers
        int statusCode = response.getStatusLine().getStatusCode();
        lastHttpStatus = statusCode;
        if (statusCode != 200) {
            JSONObject returnJ = new JSONObject();
            returnJ.put("success", false);
            returnJ.put("data", new JSONArray());

            Log.d("Http Status Code", String.valueOf(statusCode));

            switch (statusCode) {
            case 401:
                if (refreshToken() && firstAuthCall) {
                    firstAuthCall = false;
                    Utils utils = new Utils();
                    params.put("access_token", utils.getToken(context));
                    return call(controller, action, params);
                }

                returnJ.put("message", context.getString(R.string.session_expired));
                Utils utils = new Utils();
                utils.setToken(context, "", "");
                Intent intent = new Intent(context, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                return returnJ;
            case 404:
                returnJ.put("message", context.getString(R.string.action_not_found));
                return returnJ;
            case 500:
                returnJ.put("message", context.getString(R.string.server_error));
                return returnJ;
            default:
                returnJ.put("message", context.getString(R.string.internal_error));
                return returnJ;
            }
        }

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String json = reader.readLine();
        JSONTokener tokener = new JSONTokener(json);

        return new JSONObject(tokener);

    } catch (ClientProtocolException e) {

        // TODO Auto-generated catch block

    } catch (JSONException e) {

        // TODO Auto-generated catch block

    } catch (IOException e) {

        // TODO Auto-generated catch block
    }
    return new JSONObject();
}

From source file:org.bishoph.oxdemo.util.OXLoginAction.java

@Override
protected JSONObject doInBackground(Object... params) {
    HttpPost httppost = new HttpPost((String) params[0]);
    try {//www  . j a v a2  s  .  c o  m
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);

        nameValuePairs.add(new BasicNameValuePair("name", (String) params[1]));
        nameValuePairs.add(new BasicNameValuePair("password", (String) params[2]));
        nameValuePairs.add(new BasicNameValuePair("client", "OXDemo client"));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        Log.d("OXDemo", "Execute post request " + params[0]);

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost, localcontext);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity);
        Log.d("OXDemo", "<<<<<<<\n" + result + "\n\n");
        JSONObject jsonObj = new JSONObject(result);
        return jsonObj;
    } catch (ClientProtocolException e) {
        Log.e("OXDemo", e.getMessage());
    } catch (IOException e) {
        Log.e("OXDemo", e.getMessage());
    } catch (JSONException e) {
        Log.e("OXDemo", e.getMessage());
    }
    return null;
}