Android访问php webservice简单一例(2)

则可以把数据表输出为JSON或者XML格式了.客户端的ANDROID调用:

try {                                HttpParams httpParams = new BasicHttpParams();                HttpConnectionParams.setConnectionTimeout(httpParams,                        TIMEOUT_MILLISEC);                HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);                                HttpParams p = new BasicHttpParams();                                p.setParameter("user""1");                                   HttpClient httpclient = new DefaultHttpClient(p);                String url = "http://10.0.2.2:8082/myphp/phpWebservice/webservice1.php?user=1&format=json";                HttpPost httppost = new HttpPost(url);                                   try {                    Log.i(getClass().getSimpleName(), "send  task - start");                                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(                            2);                    nameValuePairs.add(new BasicNameValuePair("user""1"));                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));                    ResponseHandler<String> responseHandler = new BasicResponseHandler();                    String responseBody = httpclient.execute(httppost,                            responseHandler);                    // 解析JSON返回的                JSONObject json = new JSONObject(responseBody);                    JSONArray jArray = json.getJSONArray("posts");                    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();                       for (int i = 0; i < jArray.length(); i++) {                        HashMap<String, String> map = new HashMap<String, String>();                        JSONObject e = jArray.getJSONObject(i);                        String s = e.getString("post");                        JSONObject jObject = new JSONObject(s);                           map.put("idusers", jObject.getString("idusers"));                        map.put("UserName", jObject.getString("UserName"));                        map.put("FullName", jObject.getString("FullName"));                           mylist.add(map);                    }                    Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wydwzj.html