Example usage for org.apache.http.params HttpParams setParameter

List of usage examples for org.apache.http.params HttpParams setParameter

Introduction

In this page you can find the example usage for org.apache.http.params HttpParams setParameter.

Prototype

HttpParams setParameter(String str, Object obj);

Source Link

Usage

From source file:org.odk.collect.android.tasks.InstanceUploaderTask.java

@Override
protected ArrayList<String> doInBackground(String... values) {

    ArrayList<String> uploadedIntances = new ArrayList<String>();
    int instanceCount = values.length;

    for (int i = 0; i < instanceCount; i++) {

        publishProgress(i + 1, instanceCount);

        // configure connection
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, GlobalConstants.CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, GlobalConstants.CONNECTION_TIMEOUT);
        HttpClientParams.setRedirecting(params, false);
        params.setParameter("http.useragent", "hct's android (ODK collect) file uploads");

        // setup client
        DefaultHttpClient httpclient = new DefaultHttpClient(params);
        HttpPost httppost = new HttpPost(mUrl);
        byte[] bytes = mAuthCredentials.getBytes();
        httppost.setHeader("Authorization", new String(Base64.encodeBase64(bytes)));

        // get instance file
        File file = new File(values[i]);

        // find all files in parent directory
        File[] files = file.getParentFile().listFiles();
        if (files == null) {
            Log.e(t, "no files to upload");
            cancel(true);//from   ww  w .  j a v a2s .c o m
        }

        // mime post
        MultipartEntity entity = new MultipartEntity();
        for (int j = 0; j < files.length; j++) {
            File f = files[j];
            if (f.getName().endsWith(".xml")) {
                // uploading xml file
                entity.addPart("xml_submission_file", new FileBody(f));
                Log.i(t, "added xml file " + f.getName());
            } else if (f.getName().endsWith(".png") || f.getName().endsWith(".jpg")) {
                // upload image file
                entity.addPart(f.getName(), new FileBody(f));
                Log.i(t, "added image file" + f.getName());
            } else {
                Log.e(t, "unsupported file type, not adding file: " + f.getName());
            }
        }
        httppost.setEntity(entity);

        // prepare response and return uploaded
        HttpResponse response = null;
        System.out.println(mUrl);
        try {
            response = httpclient.execute(httppost);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return uploadedIntances;
        } catch (IOException e) {
            e.printStackTrace();
            return uploadedIntances;
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return uploadedIntances;
        }

        // check response.
        String serverLocation = null;
        Header[] h = response.getHeaders("Location");
        if (h != null && h.length > 0) {
            serverLocation = h[0].getValue();
        } else {
            // something should be done here...
            Log.e(t, "Location header was absent");
        }
        int responseCode = response.getStatusLine().getStatusCode();
        Log.e(t, "Response code:" + responseCode);

        // verify that your response came from a known server
        if (serverLocation != null && mUrl.contains(serverLocation) && responseCode == 200) {
            uploadedIntances.add(values[i]);
        }

    }

    return uploadedIntances;
}