You can use library for upload file, i am using android async http. For example:
public void UploadFile(CarrierFileInformation carrierFileInformation)
{
String path = carrierFileInformation.GetSaveDir();
File file = new File(path);
if(!file.exists())
{
this.uploadedFile = true;
Toast.makeText(this.activity, this.activity.getResources().getString(R.string.not_found_file), Toast.LENGTH_LONG).show();
return;
}
String link = Settings.DOMAIN + MethodNames.UPLOAD_FILE;
RequestParams requestParams = new RequestParams();
try
{
requestParams.put(FieldNames.UPLOAD_FILE, file);
requestParams.put(FieldNames.ORDER_ID, carrierFileInformation.GetOrderId());
requestParams.put(FieldNames.TEST_ID, carrierFileInformation.GetTestId());
requestParams.put(FieldNames.TEST_DETAIL_ID, carrierFileInformation.GetTestDetailId());
requestParams.put(FieldNames.FLAG, carrierFileInformation.GetFlag());
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.post(link, requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.e("t", "işlem tamam");
uploadedFile = true;
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
uploadedFile = false;
}
});
}
requestParams using for send params your webservice, file you know your file.
you can edit my function.
In build.gradle you have to add this code compile 'com.loopj.android:android-async-http:1.4.9' in dependices bloc then sync.