利用Web服务数据的Andr​​oid应用 [英] Android app using data from webservice

查看:98
本文介绍了利用Web服务数据的Andr​​oid应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个Android应用程序,可以从互联网资源显示一些接收数据(轮询)。

I want to write an Android application that can display some data received(polled) from an internet resource.

我想我需要编写一些逻辑,将定期调用和一些终端获取数据,解析响应并显示它。是否有一个很好的教程这一切步骤?

I guess that I need to write some logic that will periodically call and get data from some endpoint, parse the response and display it. Is there a good tutorial for all this steps?

我还不是很了解的momment的Andr​​oid编程,也许最好是先从简单的东西。我只是想知道要寻找什么,同时学习了收集一些这方面的资源。

I know very little about Android programming at the momment and maybe it is better to start with something simpler. I just want to know what to look for while learning an gather some resources on this.

推荐答案

你想要做什么正在开发的 REST API 对于您的Andr​​oid应用程序提供数据。例如。您的网站有,你想要在你的应用程序中使用的一些内容,那么你可以写一个PHP脚本,只是返回特定格式的数据。

What you want to do is developing a rest api that provides data for your android app. E.g. you website has some content that you want use in your app, then you could write a php script that just returns that data in a specific format.

例如。 mysite.net/rest/fetchAllLocations.php?maybe_some_parameters

E.g. mysite.net/rest/fetchAllLocations.php?maybe_some_parameters

这将在例如返回地点JSON格式,这里是一个例子,如何看起来像:

This would return locations in e.g. json format, here is an example how that looks like:

[{\"id\":1,\"shop_lng\":8.5317153930664,\"shop_lat\":52.024803161621,\"shop_zip$c$c\":33602,\"shop_city\":\"Bielefeld\",\"shop_street\":\"Arndtstra\ße\",\"shop_snumber\":3,\"shop_name\":\"M\üller\",\"shop_desc\":\"Kaufhaus\"}]

[{"id":1,"shop_lng":8.5317153930664,"shop_lat":52.024803161621,"shop_zipcode":33602,"shop_city":"Bielefeld","shop_street":"Arndtstra\u00dfe","shop_snumber":3,"shop_name":"M\u00fcller","shop_desc":"Kaufhaus"}]

下面是一个REST API请求的例子:

Here is an example for a rest api request:

<一个href=\"http://shoqproject.supervisionbielefeld.de/public/gateway/gateway/get-shops-by-city/city/Bielefeld\" rel=\"nofollow\">http://shoqproject.supervisionbielefeld.de/public/gateway/gateway/get-shops-by-city/city/Bielefeld

所以,当你有你的REST API设置可以应对接收与你的android手机的数据。我用一个静态方法来得到这样的数据:

So when you have your rest api set up you can deal with receiving that data with your android phone. I use a static method to get this data:

public class JsonGrabber{

    public static JSONArray receiveData(){  
        String url = "your url";
        String result = "";

        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet method = new HttpGet(url);
        HttpResponse res = null;

        try {
            res = client.execute(method);
        } catch (ClientProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try{
            InputStream is = res.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
             StringBuilder sb = new StringBuilder();
             String line = null;
             while ((line = reader.readLine()) != null) {
                     sb.append(line + "\n");
             }
             is.close();
             result = sb.toString();
        }catch(Exception e){
             Log.e("log_tag", "Error converting result "+e.toString());
        }

        JSONArray jArray = null;

        try{
             jArray = new JSONArray(result);
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return jArray;
    }
}

好了多数民众赞成,一旦你有JSON格式的数据,你只需要解析:

Well thats all, once you have your data in json format you just have to parse it:

JSONArray test = (JSONArray) JsonGrabber.receiveData() 

try { 
    for(int i=0;i<test.length();i++){
    JSONObject json_data = test.getJSONObject(i);
    int id = json_data.getInt("id");
    }
}

web请求应当在另一个线程运行,因为它可以是一个耗时的过程。所以,你需要处理的AsyncTask。这里有一些资源:

The web request should run in another thread, because it can be a time consuming process. So you need to deal with AsyncTask. Here are some resources:

无痛线程
多线程性能
你好Android的教程

这篇关于利用Web服务数据的Andr​​oid应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆