Example usage for org.apache.http.client HttpClient execute

List of usage examples for org.apache.http.client HttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient execute.

Prototype

<T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler)
        throws IOException, ClientProtocolException;

Source Link

Document

Executes HTTP request using the default context and processes the response using the given response handler.

Usage

From source file:srdes.menupp.EntreeDbAdapter.java

/**
 * @brief   Create a new review using the title and body provided. If the review is
 *          successfully created return the new rowId for that review, otherwise return
 *          a -1 to indicate failure./*from   w w  w . java 2  s  .  com*/
 * 
 * @param    title the title of the note
 * 
 * @param    body the body of the note
 * 
 * @param    entree the name of the entree the review is for
 * 
 * @param    rating the rating given to the entree in the review
 * 
 * @throws    JSONException if cannot get rowID
 * 
 * @return    rowId or -1 if failed
 */
public static Long createReview(String title, String body, String entree, float rating) throws JSONException {

    //add column information to pass to database
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair(KEY_TITLE, title));
    nameValuePairs.add(new BasicNameValuePair(KEY_BODY, body));
    nameValuePairs.add(new BasicNameValuePair(KEY_ENTREE, entree));
    nameValuePairs.add(new BasicNameValuePair(KEY_RATING, new Float(rating).toString()));

    //http post
    JSONObject response = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(INSERT_REVIEW_SCRIPT);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);
        response = new JSONObject(responseBody);

    } catch (ClientProtocolException e) {
        DebugLog.LOGD("Protocol error in http connection " + e.toString());
    } catch (UnsupportedEncodingException e) {
        DebugLog.LOGD("Encoding error in http connection " + e.toString());
    } catch (IOException e) {
        DebugLog.LOGD("IO error in http connection " + e.toString());
    }
    //rowID encoded in response
    return (Long) response.get(KEY_ROWID);
}