php android json
程序员文章站
2022-05-20 17:36:16
...
//android
public class GoWinHttp { //通过post获得数据 public static String postHttpData(String url,ListnameValuePairs) { 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; }}
ListnameValuePairs 构建 这是,post给php的参数nameValuePairs.add(new BasicNameValuePair("key","value"));
//解析方法,主要是,这里有一点特殊,就是开始第一个json对象是jsonObject 和JSONArray 有一点不同,jsonObject 是指json最开始以{开始的,而以[开始的是JSONArray ,没仔细研究,这里他们都是字符串
//解析JSON数据 public static List