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:cardgametrackercs319.DBConnectionManager.java

public static String saveTrackScores(TrackEngine engine) throws UnsupportedEncodingException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://ozymaxx.net/cs319/save_tracks_scores.php");

    post.setHeader("User-Agent", USER_AGENT);
    ArrayList<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("info", engine.getJSON()));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer result = new StringBuffer();
    String resLine = "";

    while ((resLine = reader.readLine()) != null) {
        result.append(resLine);/*from w  w  w. j av  a  2s. c  o m*/
    }

    return result.toString();
}

From source file:Main.java

public static String initializeService(String data, String serviceName, String serviceUrl) {
    Log.d("EliademyUtils", "initializeService");
    try {//  www  .j  av a 2s .c o m
        JSONObject jsObj = new JSONObject(data.toString());
        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(serviceUrl + "/login/token.php");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username", jsObj.get("username").toString()));
        nameValuePairs.add(new BasicNameValuePair("password", jsObj.get("password").toString()));
        nameValuePairs.add(new BasicNameValuePair("service", serviceName));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            StringBuilder jsonStr = new StringBuilder();
            InputStream iStream = entity.getContent();
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(iStream));
            String jpart;
            while ((jpart = bufferReader.readLine()) != null) {
                jsonStr.append(jpart);
            }
            iStream.close();
            Log.d("Moodle", jsonStr.toString());
            JSONObject jsonObj = new JSONObject(jsonStr.toString());
            return (String) jsonObj.get("token");
        }
    } catch (Exception e) {
        Log.e("EliademyUtils", "exception", e);
        return null;
    }
    return null;
}

From source file:Main.java

/**
 * Performs an HTTP Post request to the specified url with the
 * specified parameters, and return the string from the HttpResponse.
 *
 * @param url The web address to post the request to
 * @param postParameters The parameters to send via the request
 * @return The result string of the request
 * @throws Exception//from w w  w  . j  a va 2  s.  c  o m
 */
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpPost request = new HttpPost(url);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();

        String result = sb.toString();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.opencastproject.remotetest.server.resource.SearchResources.java

public static HttpResponse add(TrustedHttpClient client, String mediapackage) throws Exception {
    HttpPost post = new HttpPost(getServiceUrl() + "add");
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("mediapackage", mediapackage));
    post.setEntity(new UrlEncodedFormEntity(params));
    return client.execute(post);
}

From source file:org.opencastproject.remotetest.server.resource.SchedulerResources.java

public static HttpResponse addEvent(TrustedHttpClient client, String event) throws Exception {
    HttpPut put = new HttpPut(getServiceUrl() + "event");
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("event", event));
    put.setEntity(new UrlEncodedFormEntity(params));
    return client.execute(put);
}

From source file:io.tourniquet.junit.http.rules.examples.HttpClientHelper.java

public static HttpPost post(String url, BasicNameValuePair... params) throws UnsupportedEncodingException {

    HttpPost post = new HttpPost(url);
    if (params.length > 0) {
        post.setEntity(new UrlEncodedFormEntity(Arrays.asList(params)));
    }//from w  ww.  jav a2 s  .c  om
    return post;
}

From source file:Main.java

public static HttpEntity buildUrlEncodedFormEntity(Map<String, String> keyValuePairs) {
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();

    if (keyValuePairs != null) {
        Set<String> keys = keyValuePairs.keySet();

        for (String key : keys) {
            String value = keyValuePairs.get(key);
            BasicNameValuePair param = new BasicNameValuePair(key, value);
            params.add(param);//from   w  w  w.j  ava2s  . c  o  m
        }
    }

    try {
        return new UrlEncodedFormEntity(params);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static final String postData(String url, HashMap<String, String> params) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    String outputString = null;/*ww  w . ja  va2 s.c o  m*/

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
        Iterator it = params.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            nameValuePairs.add(new BasicNameValuePair((String) pair.getKey(), (String) pair.getValue()));
            System.out.println(pair.getKey() + " = " + pair.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) {
            byte[] result = EntityUtils.toByteArray(response.getEntity());
            outputString = new String(result, "UTF-8");
        }

    } catch (ClientProtocolException e) {
        Log.i(CLASS_NAME, "Client protocolException happened: " + e.getMessage());
    } catch (IOException e) {
        Log.i(CLASS_NAME, "Client IOException happened: " + e.getMessage());
    } catch (NetworkOnMainThreadException e) {
        Log.i(CLASS_NAME, "Client NetworkOnMainThreadException happened: " + e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        Log.i(CLASS_NAME, "Unknown exeception: " + e.getMessage());
    }
    return outputString;
}

From source file:org.opencastproject.remotetest.server.resource.DistributeResources.java

/**
 * //from  w  ww  .j  a v a  2  s  . c o  m
 * @param channel
 *          Distribution channel: local, youtube, itunesu
 * 
 */
public static HttpResponse distribute(TrustedHttpClient client, String channel, String mediapackage,
        String... elementId) throws Exception {
    HttpPost post = new HttpPost(getServiceUrl() + channel.toLowerCase() + "/");
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("mediapackage", mediapackage));
    for (String id : elementId) {
        params.add(new BasicNameValuePair("elementId", id));
    }
    post.setEntity(new UrlEncodedFormEntity(params));

    return client.execute(post);
}

From source file:com.cbtec.eliademyutils.EliademyUtils.java

public static String serviceCall(String data, String webService, String token, String serviceClient) {

    String retval = null;/*from ww  w .ja v  a  2 s. co m*/
    Log.d("EliademyUtils", "Service: " + data + " Service: " + webService);

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        if (data.toString().length() > 0) {
            JSONObject jsObj = new JSONObject(data.toString());
            nameValuePairs.addAll(nameValueJson(jsObj, ""));
        }
        nameValuePairs.add(new BasicNameValuePair("wstoken", token));
        nameValuePairs.add(new BasicNameValuePair("wsfunction", webService));
        nameValuePairs.add(new BasicNameValuePair("moodlewsrestformat", "json"));

        HttpPost httppost = new HttpPost(serviceClient + "/webservice/rest/server.php?");

        Log.d("EliademyUtils", nameValuePairs.toString());
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            StringBuilder jsonStr = new StringBuilder();
            InputStream iStream = entity.getContent();
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(iStream));
            String jpart;
            while ((jpart = bufferReader.readLine()) != null) {
                jsonStr.append(jpart);
            }
            iStream.close();
            return jsonStr.toString();
        }
    } catch (Exception e) {
        Log.e("EliademyUtils", "exception", e);
        return retval;
    }
    return retval;
}