android 数据网络存储

       在实际的项目开发过程中(应用的APP),我们用网络存储的地方比较多。今天我们一起来谈谈网络存储的功能。什么是网络存储?就是我们的数据存储在一个服务器上,app客户端类似一个URL地方的方式访问数据;在得到数据后我们可以进行解析。一般我们都会用Webservice的形式进行数据的交互。

      程序在编码的时候,需要加入java.net.*,Android.net.* 这个两个包(不然数据不能解析);网络上有好多的例子,例如天气的预报APP。我们就不讲这个了,今天呢我们讲讲从webservice 中获取数据并用Json格式解析并显示。Android系统从3.0开始包括Json的包,若以下版本自行需下载 ,地址为:http://code.google.com/p/google-gson/

     代码1 HttpUtils.java  

      传入一个url地址(webservice的地址),从服务器端获取数据。一般获取到的数据都是一个字符串。

  public class HttpUtils {   //从服务器端下载到Json数据,也就是个字符串


    public static String getData(String url) throws Exception {


        StringBuilder sb = new StringBuilder();


        HttpClient httpClient = new DefaultHttpClient();


        HttpGet httpGet = new HttpGet(url);


        HttpResponse httpResponse = httpClient.execute(httpGet);


        HttpEntity httpEntity = httpResponse.getEntity();


        if (httpEntity != null) {


            InputStream instream = httpEntity.getContent();


            BufferedReader reader = new BufferedReader(new InputStreamReader(


                    instream));


            String line = null;


            while ((line = reader.readLine()) != null) {


                sb.append(line);


            }


            return sb.toString();


        }


        return null;


    }


    代码2 JsonUtils.java 

     进行Json数据的解析

public class JsonUtils {


    public static List<Student> parseStudentFromJson(String data) {


        Type listType = new TypeToken<LinkedList<Student>>() {


        }.getType();


        Gson gson = new Gson();


        LinkedList<Student> list = gson.fromJson(data, listType);


        return list;


    }


}


    代码3 Student.java

     实际上就是定义一个JavaBean对象

public class Student {


    private String name;


    private int age;


    private String id;


 


    public Student() {


        super();


    }


 


    public Student(String name, int age, String id) {


        super();


        this.name = name;


        this.age = age;


        this.id = id;


    }


 


    public String getName() {


        return name;


    }


 


    public void setName(String name) {


        this.name = name;


    }


 


    public int getAge() {


        return age;


    }


 


    public void setAge(int age) {


        this.age = age;


    }


 


    public String getId() {


        return id;


    }


 


    public void setId(String id) {


        this.id = id;


    }


 


}

    到这里代码基本上完成了,那么我们在Android的应用程序中如何使用呢?

public class MainActivity extends Activity {


    private TextView textView;


    private List<Student> list;


 


    /** Called when the activity is first created. */


    @Override


    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);


        setContentView(R.layout.main);


        textView = (TextView) findViewById(R.id.textView);


        String data = null;


        try {


            data = HttpUtils


                    .getData("http://127.0.0.1:8080/JsonTest/getStudent");


        } catch (Exception e) {


            e.printStackTrace();


        }


        String result = "";


        list = JsonUtils.parseStudentFromJson(data);


        for (Student s : list) {


            result += "name: " + s.getName() + "   " + "age: " + s.getAge()


                    + "   " + "id: " + s.getId() + "\n";


        }


        textView.setText(result);


    }


}


    最后运行的效果怎么样呢,来看看(可以按照上面代码,大家动手做个demo):


    最后在补充一句,可能有同学按照以上代码没有运行成功。哈哈,要在AndroidManifest.xml文件中设定访问网络的权限:<uses-permission android:name="android.permission.INTERNET" />  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值