发布的问题是什么?

时间:2015-12-18 05:28:52

标签: android httpurlconnection

我一直在使用HttpUrlConnection发布视频和其他一些参数, 代码运行正常,其他它不发布数据,它能够得到 来自服务器的响应,我似乎无法弄清楚问题。 感谢任何帮助,谢谢。

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();

    }
}

0 个答案:

没有答案