如何通过android调用json webservice

我需要使用JSON以Rest格式访问.Net Web服务。 我对这个概念相当陌生,对这个工作原理非常困惑……任何人都可以给出这个概述。 我需要遵循使用JSON的步骤。 现在我的疑问是如何使用JSON来抓取输出。

Solutions Collecting From Web of "如何通过android调用json webservice"

这是parsingJson Web服务的最简单的方法

  String str="url"; try{ URL url=new URL(str); URLConnection urlc=url.openConnection(); BufferedReader bfr=new BufferedReader(new InputStreamReader(urlc.getInputStream())); String line; while((line=bfr.readLine())!=null) { JSONArray jsa=new JSONArray(line); for(int i=0;i<jsa.length();i++) { JSONObject jo=(JSONObject)jsa.get(i); title=jo.getString("deal_title"); //tag name "deal_title",will return value that we save in title string des=jo.getString("deal_description"); } } catch(Exeption e){ } 

在Android清单中提到Internet权限

Gson库可以自动parsing你的jsonstring到对象。 简单的例子:

  Gson gson = new Gson(); int[] ints = {1, 2, 3, 4, 5}; String[] strings = {"abc", "def", "ghi"}; //(Serialization) gson.toJson(ints); ==> prints [1,2,3,4,5] gson.toJson(strings); ==> prints ["abc", "def", "ghi"] //(Deserialization) int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class); ==> ints2 will be same as ints 

以下是Android活动从Web服务读取并parsingJSON对象的代码:

 public void clickbutton(View v) { try { // http://androidarabia.net/quran4android/phpserver/connecttoserver.php // Log.i(getClass().getSimpleName(), "send task - start"); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); // HttpParams p = new BasicHttpParams(); // p.setParameter("name", pvo.getName()); p.setParameter("user", "1"); // Instantiate an HttpClient HttpClient httpclient = new DefaultHttpClient(p); String url = "http://10.0.2.2:8080/sample1/" + "webservice1.php?user=1&format=json"; HttpPost httppost = new HttpPost(url); // Instantiate a GET HTTP method 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); // Parse 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(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Log.i(getClass().getSimpleName(), "send task - end"); } catch (Throwable t) { Toast.makeText(this, "Request failed: " + t.toString(), Toast.LENGTH_LONG).show(); } } 

欲了解更多详情,请参阅http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php

你使用json数据如下:

 var a=new JSONObject(jsonData); 

http://developer.android.com/resources/tutorials/views/hello-mapview.html

使用a中的数据来构造必要的对象,并用相同的方法进行必要的操作