I want to upload file to server from the device and send some data as well on the same request by encoding in the url, i have tried but it is not working. What i am doing wrong and How can we do it?
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String existingFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recorded_audio.3gp";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String responseFromServer = "";
String urlString = "http://mywebsite.com/uploadAudio.php";
@Override
protected String doInBackground(String... params) {
String uploading_file = params[0];
try{
//------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(uploading_file));
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// Sending necessary Data(userID, tags, location)
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", ("my_data"));
JSONObject data = new JSONObject();
data.put("user_data", jsonObject);
String jsonStr = data.toString();
dos = new DataOutputStream(conn.getOutputStream());
String post_data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(jsonStr, "UTF-8");
dos.writeBytes(post_data);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + uploading_file + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necessary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e("Debug", "File is written");
fileInputStream.close();
dos.flush();
dos.close();
}catch(MalformedURLException ex){
Log.e("Debug", "error: " + ex.getMessage(), ex);
} catch(IOException ioe){
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
} catch (JSONException e) {
e.printStackTrace();
}
InputStream inputStream = null;
String line = "";
try
{
inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
if ((line = bufferedReader.readLine()) != null) {
Log.e("Debug", "Server Response " + line);
}
bufferedReader.close();
inputStream.close();
conn.disconnect();
} catch(
IOException e)
{
e.printStackTrace();
}
return line;
}
}
UploadAudio uploadAudio = new UploadAudio();
uploadAudio.execute(existingFileName);
This code i have written for uploading file and POST data through URL. I have also created a php file for server.