Oterman001 2016-06-06 03:22 采纳率: 0%
浏览 1688
已结题

要崩溃了,使用httpclient上传图片得到的响应好怪异

最近在做一个安卓客户端,要求实现向服务器上传图片功能,最开始使用Asynchttpclient来上传
能够上传,onSuccess方法也能够执行,但是responseBody却没有值,本应该有值得。
这是使用Asynchttpclient的代码:

        AsyncHttpClient ahc=new AsyncHttpClient();

        String url=Constants.getUploadUrl();
    //  ahc.addHeader("Content-Type", "multipart/form-data");

        String cookie=BaseApplication.getCookie();
        if(cookie==null){
            cookie=BaseApplication.autoLogin(context, true);
        }
        //njubbs_upload36649831.jpg
        ahc.addHeader("Cookie", cookie);
        RequestParams rp=new RequestParams();
        try {
            rp.put("up", file);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        rp.put("exp", "");
        rp.put("ptext", "text");
        rp.put("board", "Pictures");
        rp.setForceMultipartEntityContentType(true);

        ahc.post(url, rp,new AsyncHttpResponseHandler() {
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                /*
                responseBody is empty. but should not be empty. the responseBody shouled be like this:

                    <meta http-equiv='Refresh' content='0; url=bbsupload2?board=Pictures
                    &file=10104&name=LilyDroid0605184406.jpg&exp=UploadByLilyDroid
                    &ptext=
                    '>

                but i got nothing! so confused. somebody help me.
                */
                LogUtil.d("结果:"+statusCode);
                waitDialog.dismiss();
                if(statusCode==200){
                    try {
                        String result=new String(responseBody,"gb2312");
                        LogUtil.d("result:"+result);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            public void onFailure(int statusCode,
                    Header[] headers,
                    byte[] responseBody, Throwable error) {
                waitDialog.dismiss();
                LogUtil.d("结果:"+statusCode+ new String(responseBody).toString());
                error.printStackTrace();
            }

        });

使用浏览器观察htttp请求,下面是观察的情况:
图片说明

这是浏览器的响应:

 <meta http-equiv='Refresh' content='0; url=bbsupload2?board=Pictures
&file=30059&name=LilyDroid0603214646.jpg&exp=
&ptext=text
'>

这个响应我根本就得不到啊!
然后我更换为httpclient来重新上传图片:
这是我使用httpclient的代码:

                HttpClient httpClient=new DefaultHttpClient();
                try {
                    String url=Constants.getUploadUrl();
                    HttpPost httpPost=new HttpPost(url);

                    FileBody fb=new FileBody(file,"image/jpeg");
                    String cookie=BaseApplication.getCookie();
                    if(cookie==null){
                        cookie=BaseApplication.autoLogin(context, true);
                    }

                    httpPost.setHeader("User-Agent","SOHUWapRebot");
                    httpPost.addHeader("Cookie", cookie);
                    httpPost.addHeader("Content-Type", "multipart/form-data");
                    MultipartEntity me=new MultipartEntity();

                    StringBody sb1=new StringBody("");
                    StringBody sb2=new StringBody("text",Charset.forName("GB2312"));
                    StringBody sb3=new StringBody("Pictures",Charset.forName("GB2312"));

                    me.addPart("up",fb);
                    me.addPart("exp", sb1);
                    me.addPart("ptext",sb2);
                    me.addPart("board", sb3);


                    httpPost.setEntity(me);
                    HttpResponse response=httpClient.execute(httpPost);

                    int statusCode = response.getStatusLine().getStatusCode();  

                     if(statusCode == HttpStatus.SC_OK){  
                            System.out.println("服务器正常响应.....");  
                            HttpEntity resEntity = response.getEntity();  
                            InputStream inputStream = resEntity.getContent();

                            BufferedReader br=new BufferedReader(new InputStreamReader(inputStream,"gbk"));
                            //nju_bbs160605153704.jpg
                            String line=null;
                            StringBuffer sb=new StringBuffer();

                            while((line=br.readLine())!=null){
                                sb.append(line);
                                sb.append("\n");
                            }
                            String result=sb.toString();

                                            。。。。。

这次我能够得到结果,但是得到的结果像下面这样子:

 <meta http-equiv='Refresh' content='0; url=bbsupload2?board=
&file=17634&name=nju_bbs160605185633.jpg&exp=Content-Transfer-Encoding: 8bit
&ptext=Content-Disposition: form-data; name="p'>

对比浏览器的结果:

 <meta http-equiv='Refresh' content='0; url=bbsupload2?board=Pictures
&file=30059&name=LilyDroid0603214646.jpg&exp=
&ptext=text
'>

两个结果不一样啊!为甚么会这样子!
调试了一整天了,都没有搞定。简直要崩溃了。
求大神帮忙解决下。感谢万分!

  • 写回答

2条回答 默认 最新

  • Oterman001 2016-06-06 03:23
    关注

    崩溃啊!调试了两天了,求大神帮忙解决啊!

    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程