0

I have been using HttpUrlConnection to post a video and some other parameter, the code runs fine else offcourse it does not post the data,it is able to get the response from the server and i cant seem to figure out the issue. Any help is appreciated ,Thankyou.

class MyAsyncTask extends AsyncTask<String, Void, String> {
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    ProgressDialog dialog;
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    int serverResponseCode = 0;
    String line = null;
    String floatMessage = null;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(EventsActivity.this);
        dialog.show();
        dialog.setMessage("Uploading Event");
        dialog.setCancelable(false);
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... urls) {
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(videopath));

            URL url = new URL("http://workintelligent.com/TagFrame/webservice/upload_video");
            connection = (HttpURLConnection) url.openConnection();
            // Allow Inputs &amp; Outputs.

            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);

            // Set HTTP method to POST.
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            connection.setRequestProperty("video_file", videopath);


            Log.e("getting user_id", userid);
            connection.setRequestProperty("user_id",userid);
            connection.setRequestProperty("access_type  ", "public");
            connection.setRequestProperty("title", "sdfdsf");
            connection.setRequestProperty("description", "sdscfsdf");
            connection.setRequestProperty("tags_keywords", "asdf");
            connection.setRequestProperty("price", "0");
            connection.setRequestProperty("is_paid", "0");

            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"video_file\";filename=\"" + videopath + "\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);



            connection.setRequestMethod("GET");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder stringBuilder = new StringBuilder();
            fileInputStream.close();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line + '\n');
            }

            String jsonString = stringBuilder.toString();
            Log.e("jsonString", jsonString);
            JSONObject resJson = new JSONObject(jsonString);
            String floatMessage = resJson.getString("upload");
            Log.e("floatMessage", floatMessage);
            outputStream.flush();
            outputStream.close();
        } catch (Exception ex) {
        }
        return floatMessage;
    }

    protected void onPostExecute(String result) {
        dialog.cancel();
        super.onPostExecute(result);
        Toast.makeText(EventsActivity.this, floatMessage, Toast.LENGTH_LONG).show();

    }
}
karan vs
  • 3,044
  • 4
  • 19
  • 26
  • i am getting the response from server in the form of json , just a validation (status="error") which is triggered just in case ,the parameter is not posted. – karan vs Dec 18 '15 at 05:30
  • Alright, i did some reasearch of my own and found this:"http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post?rq=1 " this is the most complete answer. i implemented it ,works fine. – karan vs Dec 18 '15 at 08:16

0 Answers0