安卓通过httpget传入json参数,请求数据
2014-04-01 13:47
381 查看
server接口如:
http://127.0.0.1?param={
"Params" : {
"ProjectID" : "00000010"
},
"Version" : "1.0",
"SessionID" : "111111",
"Method" : "GetProject",
"AppID" : "",
"Key" : "
}
拼凑url的方法如:
public static String getProject(String projectID, String version, String sessionID, String method, String appID, String key) throws JSONException{
JSONObject jo1 = new JSONObject();
jo1.put("ProjectID", projectID);
JSONObject jo = new JSONObject();
jo.put("Params",jo1);
jo.put("Version", version);
jo.put("SessionID", sessionID);
jo.put("Method", method);
jo.put("AppID", appID);
jo.put("Key", key);
String str = jo.toString().replace("\"", "%22").replace("{", "%7b").replace("}", "%7d");
return "http://127.0.0.1?param="+str;
}
tips:在http post或get请求时,传入的参数不能有"或{或},所以需要转码。
从server发送请求得到数据:
public JSONObject getJSONObjFromUrl(String url) throws Exception {
HttpParams httpParams = new BasicHttpParams();
setHttpParams(httpParams);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
int res = httpResponse.getStatusLine().getStatusCode();
if (200 == res) {
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
jsonStr = sb.toString();
jObj = new JSONObject(jsonStr);
} else {
throw new Exception();
}
return jObj;
}
http://127.0.0.1?param={
"Params" : {
"ProjectID" : "00000010"
},
"Version" : "1.0",
"SessionID" : "111111",
"Method" : "GetProject",
"AppID" : "",
"Key" : "
}
拼凑url的方法如:
public static String getProject(String projectID, String version, String sessionID, String method, String appID, String key) throws JSONException{
JSONObject jo1 = new JSONObject();
jo1.put("ProjectID", projectID);
JSONObject jo = new JSONObject();
jo.put("Params",jo1);
jo.put("Version", version);
jo.put("SessionID", sessionID);
jo.put("Method", method);
jo.put("AppID", appID);
jo.put("Key", key);
String str = jo.toString().replace("\"", "%22").replace("{", "%7b").replace("}", "%7d");
return "http://127.0.0.1?param="+str;
}
tips:在http post或get请求时,传入的参数不能有"或{或},所以需要转码。
从server发送请求得到数据:
public JSONObject getJSONObjFromUrl(String url) throws Exception {
HttpParams httpParams = new BasicHttpParams();
setHttpParams(httpParams);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
int res = httpResponse.getStatusLine().getStatusCode();
if (200 == res) {
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
jsonStr = sb.toString();
jObj = new JSONObject(jsonStr);
} else {
throw new Exception();
}
return jObj;
}
相关文章推荐
- android 网络连接判断
- TOMCAT HTTPS访问配置应用
- 如何自己写一个网络爬虫
- Ubuntu 网络配置
- IEEE地址和网络地址
- Git 使用 HTTP 代理服务器
- 网络131第4周实验——键盘输入数据 和 数据类型
- bzoj 1927 网络流
- 驾驶机动车在高速公路上倒车、逆行、穿越中央分隔带掉头的一次记6分。 答案:错误 2013《123号令-附件2》一、机动车驾驶人有下列违法行为之一,一次记12分[重新考《科目一》]:(七)驾驶机动车在高速公路上倒车、逆行、穿越中央分隔带掉头的; 可以参考:http://zhinan.jxedt.com/info/6375.htm
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- HttpHandler路由匹配规则
- 第4周作业-数学学习工具(网络131 梁文俊)
- lighttpd通过fastcgi调用gsoap
- TCP reset
- windbg 远程调试 --(tcp模式)
- Nginx:设置为HTTP代理服务器上网
- Java安全(加密、摘要、签名、证书、SSL、HTTPS)
- java发送JSON格式的http通讯的post请求
- 证书信任管理器(用于https请求)
- Linux网络编程:原始套接字的魔力【续】