如何从这种Web服务中获取数据

时间:2013-02-11 05:58:51

标签: android json

这是我的网络服务数据。

[
    {
        "msgid": "1",
        "title": "Lorem Ipsum is simply text",
        "dt": "05-SEP-2012"
    },
    {
        "msgid": "2",
        "title": "Lorem Ipsum is simply ",
        "dt": "16-NOV-2011"
    },
    {
        "msgid": "3",
        "title": "Lorem Ipsum is simply text",
        "dt": "13-SEP-2012"
    },
    {
        "msgid": "4",
        "title": "Lorem Ipsum is simply text2",
        "dt": "26-SEP-2012"
    },
    {
        "msgid": "5",
        "title": "Lorem Ipsum is simply text",
        "dt": "30-SEP-2012"
    },
    {
        "msgid": "6",
        "title": "test message ",
        "dt": "12-OCT-2012"
    },
    {
        "msgid": "7",
        "title": "test message ",
        "dt": "01-OCT-2012"
    }
]

如何使用JSON解析在Android中将此数据提取到应用? 什么是这个数据的java代码可以帮助我.... plz ....

1 个答案:

答案 0 :(得分:0)

公共类Original_list扩展了Activity {

 TextView web;
 ArrayList<String>  id1,question1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
 web = (TextView)findViewById(R.id.web1);
 id1 = new ArrayList<String>();
 question1= new ArrayList<String>();
try {
    JSONArray jary = new JSONArray(readTxt());
    for(int i = 0 ;i<=jary.length();i++)
    {
        JSONObject jread = jary.getJSONObject(i);
        String   id = jread.getString("question");
        Log.d("value of id", ""+id);
        id1.add(id);
        String name = jread.getString("answer");
        Log.d("value of id", ""+id);
        question1.add(name);
        Log.e("id","question is"+id);
      Log.e("id","question is"+name);
    }
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}   
 web.setText(""+id1+""+question1);
    web.setMovementMethod(new ScrollingMovementMethod());
}

private String readTxt(){

         InputStream inputStream = getResources().openRawResource(R.raw.details);

         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

         int i;
      try {
       i = inputStream.read();
       System.out.println("what"+i);
       while (i != -1)
          {
           byteArrayOutputStream.write(i);
           i = inputStream.read();
           System.out.println("what_next"+i);
          }
          inputStream.close();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

         return byteArrayOutputStream.toString();
        }

}