Search in sources :

Example 6 with FilePart

use of com.android.internal.http.multipart.FilePart in project android_frameworks_base by ResurrectionRemix.

the class BandwidthTestUtil method postFileToServer.

/**
     * Post a given file for a given device and timestamp to the server.
     * @param server {@link String} url of test server
     * @param deviceId {@link String} device id that is uploading
     * @param timestamp {@link String} timestamp
     * @param file {@link File} to upload
     * @return true if it succeeded
     */
public static boolean postFileToServer(String server, String deviceId, String timestamp, File file) {
    try {
        Log.d(LOG_TAG, "Uploading begining");
        HttpClient httpClient = new DefaultHttpClient();
        String uri = server;
        if (!uri.endsWith("/")) {
            uri += "/";
        }
        uri += "upload";
        Log.d(LOG_TAG, "Upload url:" + uri);
        HttpPost postRequest = new HttpPost(uri);
        Part[] parts = { new StringPart("device_id", deviceId), new StringPart("timestamp", timestamp), new FilePart("file", file) };
        MultipartEntity reqEntity = new MultipartEntity(parts, postRequest.getParams());
        postRequest.setEntity(reqEntity);
        HttpResponse res = httpClient.execute(postRequest);
        res.getEntity().getContent().close();
    } catch (IOException e) {
        Log.e(LOG_TAG, "Could not upload file with error: " + e);
        return false;
    }
    return true;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntity(com.android.internal.http.multipart.MultipartEntity) Part(com.android.internal.http.multipart.Part) FilePart(com.android.internal.http.multipart.FilePart) StringPart(com.android.internal.http.multipart.StringPart) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) StringPart(com.android.internal.http.multipart.StringPart) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) FilePart(com.android.internal.http.multipart.FilePart)

Aggregations

FilePart (com.android.internal.http.multipart.FilePart)6 MultipartEntity (com.android.internal.http.multipart.MultipartEntity)6 Part (com.android.internal.http.multipart.Part)6 StringPart (com.android.internal.http.multipart.StringPart)6 IOException (java.io.IOException)6 HttpResponse (org.apache.http.HttpResponse)6 HttpClient (org.apache.http.client.HttpClient)6 HttpPost (org.apache.http.client.methods.HttpPost)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6