如何通过android调用json webservice

时间:2011-08-16 10:09:41

标签: java android web-services json

我需要使用JSON以Rest格式访问.Net Web服务。我 对这个概念很陌生,并且非常混淆这个怎么样 作品.... 任何人都可以对此进行概述。我需要我的步骤 需要遵循使用JSON。现在我的疑问是如何使用JSON 抓住输出。

4 个答案:

答案 0 :(得分:4)

这是解析Json网络服务

的最简单方法
    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权限

答案 1 :(得分:2)

Gson库可以自动将你的json字符串解析为object。 简单的例子:

 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

答案 2 :(得分:0)

您使用json数据如下:

var a=new JSONObject(jsonData);

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

使用a中的数据构建必要的对象并使用相同的

执行必要的操作

答案 3 :(得分:0)

以下是从Web Service读取并解析JSON对象的Android活动的代码:

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