论坛首页 移动开发技术论坛

Android(摩托)HttpURLConnection getInputStream()堵塞

浏览 3001 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-08-25  
各位大侠
     上传文件到服务器,使用HttpURLConnection 上传,上传一半时,此时断网,摩托手机收不到任何异常,此线程则被一直堵塞在此,各位帮忙看看代码。
       URL url = new URL(actionUrl);
        try
        {
            connect = (HttpURLConnection) url.openConnection();

            connect.setDoOutput(true);
            connect.setDoInput(true);
            connect.setRequestMethod(httpMethod);
            connect.setRequestProperty("Authorization",
                "Basic " + authorization);
            connect.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=NextPart_000_0028_01C198.8469");
            connect.setRequestProperty("Accept-Encoding",
                "gzip");
            connect.setConnectTimeout(15 * 1000);
            connect.setReadTimeout(10 * 1000);
            connect.connect();
            if (request != null)
            {
                 output = new DataOutputStream(connect.getOutputStream());
                LogUtil.d(TAG,
                    "sendMMsRequest >> 开始添加附件");
                addFormField(filePathList,
                    request,
                    output,
                    sendContentStr);

                String lineEnd = "--NextPart_000_0028_01C198.8469--";
                output.writeBytes(lineEnd);
                LogUtil.d(TAG,
                    "sendMMsRequest >> 结束添加附件 ");
                LogUtil.d(TAG,
                    "sendMMsRequest >> 开始往服务器写入数据");
                output.flush();
                LogUtil.d(TAG,
                    "sendMMsRequest >> 结束往服务器写入数据");
            }
            filePathList.clear();
            LogUtil.d(TAG,
                "sendMMsRequest >> 开始读取服务器数据");
            is = connect.getInputStream();//此处堵塞
            dis = new DataInputStream(is);
            LogUtil.d(TAG,
                "sendMMsRequest >> 获得inputStream对象");

            int n = 0;
            while ((n = dis.read(readBuffer)) != -1)
            {
                LogUtil.d(TAG,
                    "sendMMsRequest >> 读取服务器数据" + readBuffer.length);
                result += new String(readBuffer,
                    0,
                    n);
            }
            LogUtil.d(TAG,
                "sendMMsRequest >> 结束读取服务器数据");






日志打印如下

08-25 01:07:05.484: sendMMsRequest >> 开始添加附件
08-25 01:07:05.491: sendMMsRequest >> 添加附件 20120817153822.jpg
08-25 01:07:06.069: sendMMsRequest >> 添加附件 561.png
08-25 01:07:06.069: sendMMsRequest >> 添加附件 d_YaoYao.mp3
08-25 01:07:07.069: sendMMsRequest >> 结束添加附件
08-25 01:07:07.069: sendMMsRequest >> 开始往服务器写入数据
08-25 01:07:07.069: sendMMsRequest >> 结束往服务器写入数据
08-25 01:07:07.069: sendMMsRequest >> 开始读取服务器数据
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics