您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php android json

2024/5/31 0:26:45发布25次查看
//android
public class gowinhttp { //通过post获得数据 public static string posthttpdata(string url,list namevaluepairs) { string resultstr=null; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); try { httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response; response=httpclient.execute(httppost); resultstr=entityutils.tostring(response.getentity()); } catch (unsupportedencodingexception e) { log.d(url, unsupportedencodingexception); e.printstacktrace(); } catch (clientprotocolexception e) { log.d(url, clientprotocolexception); e.printstacktrace(); } catch (ioexception e) { log.d(url, ioexception); e.printstacktrace(); } return resultstr; }}
list namevaluepairs 构建 这是,post给php的参数namevaluepairs.add(new basicnamevaluepair(key,value));
//解析方法,主要是,这里有一点特殊,就是开始第一个json对象是jsonobject 和jsonarray 有一点不同,jsonobject 是指json最开始以{开始的,而以[开始的是jsonarray ,没仔细研究,这里他们都是字符串
//解析json数据 public static listresolvedata(string jsonstr) { listls=new arraylist(); map data; jsonobject jsonobject; jsonarray jsonarr; jsonarray namearr; try { jsonarr=new jsonarray(jsonstr); for(int i=0;i //php上面不管以任何方式得到一个数组,然后将数组编码成为json格式
//这里有一个方法可以将一整张表转换为数组
//然后php只需要将得到的数组格式化为json就行了
$json_string = json_encode($resarr);echo $json_string;
//超时请求 httpclient.getparams().setparameter(coreconnectionpnames.connection_timeout, 5000); //读取超时 httpclient.getparams().setparameter(coreconnectionpnames.so_timeout, 5000);
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product