am I uploading this file right? Here is my code:
Deleted Code
it is basically just a async task that sends the file (csv) to the web server.
I am getting back status code 400 🙁 Anyone know what I am doing wrong?
Edit: Now I am getting back status code 411, but when I specify the content-length it comes back with ClientProtocolException.
Here’s my code now:
UploadTask uploadtask;
public class UploadTask extends AsyncTask<Void, byte[], Boolean> {
HttpPost httppost;
@Override
protected Boolean doInBackground(Void... params) {
Boolean result = false;
String id = projectIDs.get((int) spinner.getSelectedItemId());
Log.i("TAG", id);
try {
l(send(String.valueOf(id), "http://" + site
+ "/restlet/position.csv?project=" + id,
"/csv.csv"));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
private void entity(String id, String file) throws JSONException,
UnsupportedEncodingException, FileNotFoundException {
// Add your data
File myFile = new File(Environment.getExternalStorageDirectory(),
file);
FileEntity fileEntity = new FileEntity(myFile, "multipart/form-data;");
fileEntity.setChunked(true);
long len = fileEntity.getContentLength();
httppost.getParams().setParameter("project", id);
httppost.setEntity(fileEntity);
//httppost.addHeader("Content-Length",String.valueOf(len));
httppost.setHeader("Content-Length", String.valueOf(len));
}
private String send(String id, String URL, String file)
throws ClientProtocolException, IOException, JSONException {
l(URL);
HttpResponse response = null;
httppost = new HttpPost(URL);
entity(id, file);
response = httpclient.execute(httppost);
Header[] head = response.getAllHeaders();
String str = String.valueOf(response.getStatusLine()
.getStatusCode());
response.getEntity().consumeContent();
return str;
}
}
This code here worked for uploading a file to the web server 🙂 After many hours of struggle I got it, had to import MultipartEntity, StringBody and FileBody from apache however