当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to fetch data from this kind of web service

问题描述:

This is my web service data.

[

{

"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"

}

]

How can I fetch this data to app in Android using JSON parsing?

what is the java code for this data can u help me....plz....

网友答案:

public class Original_list extends 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();
        }

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: