话说为啥我的问题基本就没解决过的 郁闷,闲话不多说,项目中有用到与服务器交互,之前都是自己写的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,所以不知道怎么用了,求指教