话说为啥我的问题基本就没解决过的 郁闷,闲话不多说,项目中有用到与服务器交互,之前都是自己写的httpurlconnection来进行传送数据,因为服务端要接受的是一个压缩后的Json字符串,之前使用如下方法将参数写入。
public static String httpPost(String urlAddress,String params){
URL url=null;
HttpURLConnection conn=null;
BufferedReader in=null;
StringBuffer sb=new StringBuffer();
try {
url=new URL(urlAddress);
conn=(HttpURLConnection) url.openConnection(); //建立连接
//设置通用的请求属性
/*conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");*/
conn.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
conn.setRequestProperty("Accept","*/*");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setConnectTimeout(5 * 1000);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
//conn.connect();
byte[] b=params.getBytes();
System.out.println("btye length:"+b.length);
//conn.setRequestProperty("Content-Length", String.valueOf(b.length));
conn.getOutputStream().write(b, 0, b.length);
conn.getOutputStream().flush();
conn.getOutputStream().close();
int count=conn.getResponseCode();
if (200==count) {
in=new BufferedReader(new InputStreamReader(conn.getInputStream())); //发送请求
}else {
System.out.println("错误类型:"+count);
return "server no start-up";
}
while (true) {
String line=in.readLine();
if (line==null) {
break;
}else {
sb.append(line);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("error ioexception:"+e.getMessage());
e.printStackTrace();
return "server no start-up";
}finally{
try {
if (in!=null) {
in.close();
}
if (conn!=null) {
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
但是现在想用Volley框架 因为返回值也是String类型(虽然是Json.toString),因为他的参数是压缩后的Json字符串,所以并没有对应的key,所以不知道怎么用了,求指教
封装一层,先把压缩的的东西解压缩,编码后,传给Volley
不是 是服务器就要一个压缩后的Json字符串,就是说我就有一个url 和一个值压缩后的Json字符串 没有key
Volley提供的功能
简单的讲,提供了如下主要的功能:
1、封装了的异步的RESTful 请求API;
2、一个优雅和稳健的请求队列;
3、一个可扩展的架构,它使开发人员能够实现自定义的请求和响应处理机制;
4、能够使用外部HTTP Client库;
5、缓存策略;
6、自定义的网络图像加载视图(NetworkImageView,ImageLoader等);
参数不要压缩