Search in sources :

Example 11 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project android-volley by mcxiaoke.

the class HttpClientStack method setEntityIfNonEmptyBody.

private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        HttpEntity entity = new ByteArrayEntity(body);
        httpRequest.setEntity(entity);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Example 12 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project JamsMusicPlayer by psaravan.

the class GMusicClientCalls method getPlaylistEntriesMobileClient.

/******************************************************************************************
	 * Retrieves a JSONAray with all songs in <i><b>every</b></i> playlist. The JSONArray 
	 * contains the fields of the songs such as "id", "clientId", "trackId", etc. (for a list 
	 * of all fields, see MobileClientPlaylistEntriesSchema.java). 
	 * 
	 * @deprecated This method is fully functional. However, there are issues with retrieving 
	 * the correct playlist entryIds. Specifically, the entryIds do not seem to work with 
	 * reordering playlists via the MobileClient mutations protocol. 
	 * 
	 * @return A JSONArray object that contains all songs and their fields within every playlist. 
	 * @param context The context to use while retrieving songs from the playlist.
	 ******************************************************************************************/
public static final JSONArray getPlaylistEntriesMobileClient(Context context) throws JSONException, IllegalArgumentException {
    JSONArray playlistEntriesJSONArray = new JSONArray();
    JSONObject jsonRequestParams = new JSONObject();
    jsonRequestParams.put("max-results", 10000);
    jsonRequestParams.put("start-token", "0");
    mHttpClient.setUserAgent(mMobileClientUserAgent);
    String result = mHttpClient.post(context, "https://www.googleapis.com/sj/v1.1/plentryfeed?alt=json&hl=en_US&tier=basic", new ByteArrayEntity(jsonRequestParams.toString().getBytes()), "application/json");
    JSONObject resultJSONObject = new JSONObject(result);
    JSONObject dataJSONObject = new JSONObject();
    if (resultJSONObject != null) {
        dataJSONObject = resultJSONObject.optJSONObject("data");
    }
    if (dataJSONObject != null) {
        playlistEntriesJSONArray = dataJSONObject.getJSONArray("items");
    }
    return playlistEntriesJSONArray;
}
Also used : JSONObject(org.json.JSONObject) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) JSONArray(org.json.JSONArray)

Example 13 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project JamsMusicPlayer by psaravan.

the class GMusicClientCalls method createPlaylist.

/****************************************************************************
	 * Creates a new, user generated playlist. This method only creates the 
	 * playlist; it does not add songs to the playlist.
	 * 
	 * @param context The context to use while creating the new playlist.
	 * @param playlistName The name of the new playlist.
	 * @return Returns the playlistId of the newly created playlist.
	 * @throws JSONException
	 * @throws IllegalArgumentException
	 ****************************************************************************/
public static final String createPlaylist(Context context, String playlistName) throws JSONException, IllegalArgumentException {
    JSONObject jsonParam = new JSONObject();
    JSONArray mutationsArray = new JSONArray();
    JSONObject createObject = new JSONObject();
    createObject.put("lastModifiedTimestamp", "0");
    createObject.put("name", playlistName);
    createObject.put("creationTimestamp", "-1");
    createObject.put("type", "USER_GENERATED");
    createObject.put("deleted", false);
    mutationsArray.put(new JSONObject().put("create", createObject));
    jsonParam.put("mutations", mutationsArray);
    mHttpClient.setUserAgent(mMobileClientUserAgent);
    String result = mHttpClient.post(context, "https://www.googleapis.com/sj/v1.1/playlistbatch?alt=json&hl=en_US", new ByteArrayEntity(jsonParam.toString().getBytes()), "application/json");
    mHttpClient.setUserAgent(mWebClientUserAgent);
    return new JSONObject(result).optJSONArray("mutate_response").getJSONObject(0).optString("id");
}
Also used : JSONObject(org.json.JSONObject) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) JSONArray(org.json.JSONArray)

Example 14 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project JamsMusicPlayer by psaravan.

the class GMusicClientCalls method getUserPlaylistsMobileClient.

/*******************************************************************************************
	 * Sends a POST request to Google's servers and retrieves a JSONArray with all user 
	 * playlists. The JSONArray contains the fields of the playlist such as "id", "name", 
	 * "type", etc. (for a list of all response fields, see MobileClientPlaylistsSchema.java).
	 * 
	 * @return A JSONArray object that contains all user playlists and their fields.
	 * @param context The context to use while retrieving user playlists.
	 *******************************************************************************************/
public static final JSONArray getUserPlaylistsMobileClient(Context context) throws JSONException, IllegalArgumentException {
    JSONObject jsonRequestParams = new JSONObject();
    JSONArray playlistsJSONArray = new JSONArray();
    jsonRequestParams.put("max-results", 250);
    jsonRequestParams.put("start-token", "0");
    mHttpClient.setUserAgent(mMobileClientUserAgent);
    String result = mHttpClient.post(context, "https://www.googleapis.com/sj/v1.1/playlistfeed?alt=json&hl=en_US&tier=basic", new ByteArrayEntity(jsonRequestParams.toString().getBytes()), "application/json");
    JSONObject resultJSONObject = new JSONObject(result);
    JSONObject dataJSONObject = new JSONObject();
    if (resultJSONObject != null) {
        dataJSONObject = resultJSONObject.optJSONObject("data");
    }
    if (dataJSONObject != null) {
        playlistsJSONArray = dataJSONObject.getJSONArray("items");
    }
    return playlistsJSONArray;
}
Also used : JSONObject(org.json.JSONObject) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) JSONArray(org.json.JSONArray)

Example 15 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project robovm by robovm.

the class HttpService method handleException.

protected void handleException(final HttpException ex, final HttpResponse response) {
    if (ex instanceof MethodNotSupportedException) {
        response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
    } else if (ex instanceof UnsupportedHttpVersionException) {
        response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
    } else if (ex instanceof ProtocolException) {
        response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
    } else {
        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    }
    byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage());
    ByteArrayEntity entity = new ByteArrayEntity(msg);
    entity.setContentType("text/plain; charset=US-ASCII");
    response.setEntity(entity);
}
Also used : ProtocolException(org.apache.http.ProtocolException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) UnsupportedHttpVersionException(org.apache.http.UnsupportedHttpVersionException)

Aggregations

ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)74 HttpEntity (org.apache.http.HttpEntity)25 HttpPost (org.apache.http.client.methods.HttpPost)22 HttpResponse (org.apache.http.HttpResponse)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IOException (java.io.IOException)13 JSONObject (org.json.JSONObject)10 Test (org.junit.Test)10 HttpClient (org.apache.http.client.HttpClient)8 JSONArray (org.json.JSONArray)7 InputStream (java.io.InputStream)6 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)5 BytesRef (org.apache.lucene.util.BytesRef)5 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)5 GetRequest (org.elasticsearch.action.get.GetRequest)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)5