xUtils上传图片到ASP.NET的restful问题,急~

lijingjin 发布于 2014/05/10 13:04
阅读 2K+
收藏 0

我用xutils上传图片到ASP.NET写的restful服务端,post提交的添加body时候遇到问题,请专家帮忙看看,很紧急,谢谢了。

版本xutils-2.5.5

服务端是ASP.NET的restful,代码如下:


[WebInvoke(UriTemplate = "UploadFile", Method = "POST")]
        public int UploadFile(SignInfo signInfo)
        {
            int result = 0;
            string imgname = signInfo.conf;
            string imgfile = signInfo.sign;
            return result;
        }
客户端为Android,代码如下:



//调用代码如下:
 String json="{\"conf\":\""+signInfo.getConf()+"\",\"sign\":\""+signInfo.getSign()+"\"}";
        List<String[]> list_b = new ArrayList<String[]>();
        list_b.add(new String[] { "signInfo", json });
        result = HttpPOST(url, null, list_b);

protected static String HttpPOST(String url, List<String[]> list_q,
			List<String[]> list_b) {
		String result = "";
		RequestParams p = new RequestParams();
		if (list_q != null) {
			for (String[] s : list_q) {
				p.addQueryStringParameter(s[0], s[1]);
			}
		}
		if (list_b != null) {
			for (String[] s : list_b) {
                         //添加了下面的语句后,调用报错如下:
                        //com.lidroid.xutils.exception.HttpException: Bad Request
                         p.addBodyParameter(s[0], s[1]);
			}
		}
		HttpUtils http = new HttpUtils();
		http.configCurrentHttpCacheExpiry(1000 * 20);
		try {
			ResponseStream responseStream = http.sendSync(
					HttpRequest.HttpMethod.POST, url,p);
			result = responseStream.readString();
		} catch (Exception e) {
			result = "-9";
		}
		return result;
	}

使用addBodyParameter添加就报错,用addQueryStringParameter却是正确的

报错为:com.lidroid.xutils.exception.HttpException: Bad Request

我是想上传图片,思路是将图片转成String上传到restful服务,因为统一都是用的restful,不能用.aspx或.asmx等,queryString肯定不适合,请问用xutils有什么方法能达到我的要求,正常上传图片吗?

服务端接收的参数是string,如果使用addBodyParameter(string,file),服务端接收到的是什么?该怎么写呢?

谢谢了

加载中
0
Korey
Korey
图片转String用HTTP上传会有问题,你的图片是不是有几百K
lijingjin
lijingjin
图片很小,只有几K-十几K
0
lijingjin
lijingjin

我用原始的POST上传没有问题,服务端也是上面的代码,可以成功,但是我用XUtils上传却不行。因为用的是XUtils,所以想统一都用XUtils

原始POST代码如下:

protected static String HttpPOST(String url, String strjson) {
		String result = "0", encode = "UTF-8";
		try {
			URL u = new URL(url);
			HttpURLConnection httpconn = (HttpURLConnection) u.openConnection();
			httpconn.setDoOutput(true);
			httpconn.setDoInput(true);
			httpconn.setUseCaches(false);
			httpconn.setRequestMethod("POST");

			String requestString = strjson;
			byte[] bytes = requestString.getBytes("UTF-8");
			httpconn.setRequestProperty("Content-length", "" + bytes.length);
			httpconn.setRequestProperty("Content-Type", "application/json");
			httpconn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
			httpconn.setRequestProperty("Charset", encode);
			// 建立输出流,并写入数据
			OutputStream outputStream = httpconn.getOutputStream();
			outputStream.write(bytes);
			outputStream.close();
			// 获得响应状态
			int responseCode = httpconn.getResponseCode();
			if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功
				// 当正确响应时处理数据
				StringBuffer sb = new StringBuffer();
				String readLine;
				BufferedReader responseReader;
				// 处理响应流,必须与服务器响应流输出的编码一致
				responseReader = new BufferedReader(new InputStreamReader(
						httpconn.getInputStream(), encode));
				while ((readLine = responseReader.readLine()) != null) {
					sb.append(readLine);
				}
				result = sb.toString();
				responseReader.close();
			}
		} catch (Exception e) {
			result = "-9";
		}
		return result;
	}




0
lijingjin
lijingjin
请大神来看看。 @wyouflf
0
君临天下-龙
君临天下-龙
问题解决了吗楼主?
返回顶部
顶部