最近在做一个安卓客户端,要求实现向服务器上传图片功能,最开始使用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
'>
两个结果不一样啊!为甚么会这样子!
调试了一整天了,都没有搞定。简直要崩溃了。
求大神帮忙解决下。感谢万分!