Example usage for org.apache.http.message BasicNameValuePair BasicNameValuePair

List of usage examples for org.apache.http.message BasicNameValuePair BasicNameValuePair

Introduction

In this page you can find the example usage for org.apache.http.message BasicNameValuePair BasicNameValuePair.

Prototype

public BasicNameValuePair(String str, String str2) 

Source Link

Usage

From source file:Main.java

public static void PostRequest(String url, Map<String, String> params, String userName, String password,
        Handler messageHandler) {
    HttpPost postMethod = new HttpPost(url);
    List<NameValuePair> nvps = null;
    DefaultHttpClient client = new DefaultHttpClient();

    if ((userName != null) && (userName.length() > 0) && (password != null) && (password.length() > 0)) {
        client.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(userName, password));
    }/*from   w  w w  . java  2s  .  c  o m*/

    final Map<String, String> sendHeaders = new HashMap<String, String>();
    sendHeaders.put(CONTENT_TYPE, MIME_FORM_ENCODED);

    client.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            for (String key : sendHeaders.keySet()) {
                if (!request.containsHeader(key)) {
                    request.addHeader(key, sendHeaders.get(key));
                }
            }
        }
    });

    if ((params != null) && (params.size() > 0)) {
        nvps = new ArrayList<NameValuePair>();
        for (String key : params.keySet()) {
            nvps.add(new BasicNameValuePair(key, params.get(key)));
        }
    }
    if (nvps != null) {
        try {
            postMethod.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    ExecutePostRequest(client, postMethod, GetResponseHandlerInstance(messageHandler));
}

From source file:bkampfbot.state.Opponent.java

public final static Opponent getByName(String name) throws NotFound {
    Opponent o = new Opponent();
    o.setName(name);//from ww  w.j  a va  2  s  . co  m

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("playerName", name));
    try {
        JSONObject ob = Utils.getJSON("fights/searchCharacterJson/1", nvps);

        JSONArray arr = ob.getJSONArray("list");
        for (int i = 0; i < arr.length(); i++) {
            JSONObject player = arr.getJSONObject(i);
            if (player.getString("name").equalsIgnoreCase(name)) {
                o.setAttack(player.getString("attack"));
                return o;
            }
        }

    } catch (JSONException e) {
    }

    throw new NotFound();
}

From source file:br.cefetrj.sagitarii.nunki.comm.WebClient.java

public void doPost(String action, String parameter, String content) throws Exception {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost httppost = new HttpPost(gf.getHostURL() + "/" + action);

    // Request parameters and other properties.
    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair(parameter, content));
    httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

    httpClient.execute(httppost);/* w ww. java2s  . c  o m*/
    httpClient.close();

}

From source file:com.lexicalintelligence.admin.add.AddEntryRequest.java

public AddResponse execute() {
    List<BasicNameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("name", entry.getName()));
    params.add(new BasicNameValuePair("synonym", entry.getSynonym()));
    params.add(new BasicNameValuePair("matchWordOrder", Boolean.toString(entry.isOrderSensitive())));
    params.add(new BasicNameValuePair("caseSensitive", Boolean.toString(entry.isCaseSensitive())));
    params.add(new BasicNameValuePair("matchStopwords", Boolean.toString(entry.isMatchStopwords())));
    params.add(new BasicNameValuePair("matchPunctuation", Boolean.toString(entry.isMatchPunctuation())));
    params.add(new BasicNameValuePair("stem", Boolean.toString(entry.isStemmed())));

    AddResponse addResponse;/*from  w  w  w . ja v  a  2  s. co m*/
    Reader reader = null;
    try {
        HttpResponse response = client.getHttpClient().execute(new HttpGet(client.getUrl() + PATH + getPath()
                + "?" + URLEncodedUtils.format(params, StandardCharsets.UTF_8)));
        reader = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8);
        addResponse = new AddResponse(Boolean.valueOf(IOUtils.toString(reader)));
    } catch (Exception e) {
        addResponse = new AddResponse(false);
        log.error(e);
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
            log.error(e);
        }
    }
    return addResponse;
}

From source file:de.mojadev.ohmmarks.virtuohm.http.VirtuOhmParams.java

public static HttpEntity getSessionRegisterParams(String in_crimmo, String userid)
        throws UnsupportedEncodingException {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(USER_ID, userid));
    params.add(new BasicNameValuePair(IN_CRIMMO, in_crimmo));
    return new UrlEncodedFormEntity(params);

}

From source file:com.farsunset.cim.util.MessageDispatcher.java

private static String httpPost(String url, Message msg) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();

    nvps.add(new BasicNameValuePair("mid", msg.getMid()));
    nvps.add(new BasicNameValuePair("extra", msg.getExtra()));
    nvps.add(new BasicNameValuePair("action", msg.getAction()));
    nvps.add(new BasicNameValuePair("title", msg.getTitle()));
    nvps.add(new BasicNameValuePair("content", msg.getContent()));
    nvps.add(new BasicNameValuePair("sender", msg.getSender()));
    nvps.add(new BasicNameValuePair("receiver", msg.getReceiver()));
    nvps.add(new BasicNameValuePair("timestamp", String.valueOf(msg.getTimestamp())));

    httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
    CloseableHttpResponse response2 = httpclient.execute(httpPost);
    String data = null;/* w  w  w . j av a  2 s .  c om*/
    try {
        System.out.println(response2.getStatusLine());
        HttpEntity entity2 = response2.getEntity();
        data = EntityUtils.toString(entity2);
    } finally {
        response2.close();
    }

    return data;
}

From source file:com.emobc.android.profiling.Profile.java

public static List<NameValuePair> createNamedParameters(Context context) {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    SharedPreferences settings = getProfileData(context);
    Map<String, ?> map = settings.getAll();
    for (String key : map.keySet()) {
        final String value = map.get(key).toString();
        if (value != null && value.length() > 0) {
            nameValuePairs.add(new BasicNameValuePair(key, value));
        }/*from   w  ww .j a v a2s .c  o m*/
    }
    return nameValuePairs;
}

From source file:com.groupxs.cordova.plugin.backgroundgpssend.AppService.java

@Override
protected void doWakefulWork(Intent intent) {
    String token = intent.getStringExtra("token");

    try {/*from   w ww.  j a va  2 s .  c  o  m*/
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(8);
        nameValuePairs.add(new BasicNameValuePair("Sender", token));
        nameValuePairs.add(new BasicNameValuePair("Latitude", "0"));
        nameValuePairs.add(new BasicNameValuePair("Longitude", "0"));
        nameValuePairs.add(new BasicNameValuePair("Accuracy", "0"));
        nameValuePairs.add(new BasicNameValuePair("Altitude", "0"));
        nameValuePairs.add(new BasicNameValuePair("Heading", "0"));
        nameValuePairs.add(new BasicNameValuePair("Speed", "0"));
        nameValuePairs.add(new BasicNameValuePair("AltitudeAccuracy", "0"));

        getJSONfromURL("https://localhost/api/GpsUpdate", nameValuePairs);
    } catch (Exception e) {
        Log.d("CordovaPlugin", "Exception sending data to server");
    }
}

From source file:dk.i2m.drupal.util.HttpMessageBuilder.java

private Set<NameValuePair> prep() {
    checkLang();//from w w  w.  j a  v  a  2  s  . c o m

    Set<NameValuePair> set = new HashSet<NameValuePair>();
    set.add(new BasicNameValuePair("language", language));

    for (NameValuePair nvp : nvps) {
        if ("language".equals(nvp.getName())) {
            continue; // Ignore
        }

        set.add(nvp);
    }

    return set;
}

From source file:org.utils.HttpDriver.java

/**
 *
 * @param mainvars/*from ww  w  . j a v a  2s  . co m*/
 * @return 
 */
public String Start() {
    Log.d(LOG_TAG, "Destination Url," + Url);
    Log.d(LOG_TAG, "Post Send Json," + Json);
    String responseBody = null;
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(Url.toString());
    CookieHandler.setDefault(new CookieManager());
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("Json", this.Json));
        // nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        httppost.setHeader("Host", "78.46.251.139");
        httppost.setHeader("User-Agent", USER_AGENT);
        httppost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httppost.setHeader("Accept-Language", "en-US,en;q=0.5");
        httppost.setHeader("Cookie", cookies + ";");
        httppost.setHeader("Connection", "keep-alive");
        httppost.setHeader("Referer", "https://accounts.google.com/ServiceLoginAuth");
        httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

        Log.d(LOG_TAG, "Post Send Cookies," + cookies + ";");

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        int responseCode = response.getStatusLine().getStatusCode();
        Log.d(LOG_TAG, "Response code," + responseCode);
        switch (responseCode) {
        case 200:
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseBody = EntityUtils.toString(entity);
            }
            break;
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    return (responseBody);

}