如何在android上使用Intent putExtra JSON数据

时间:2016-08-29 09:24:56

标签: android json

最近,在我的网络服务器上,为了获取JSON文本,我使用AsyncTask

    String strData = "";

@Override
protected void onPostExecute(String result) {
   super.onPostExecute(result);

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
try {
    JSONArray arr =new JSONArray(result);
       JSONObject ob = arr.getJSONObject(0);       
       strData += ob.getString("name") + "\n"
               + " - " + ob.getString("school") + "\n"
               + " - " + ob.getString("job") + "\n\n";
       adapter.add(strData);
} catch (JSONException e) {
   Log.d(TAG,"DD");
} 
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}

结果

aaa
- seoul university
- teacher

我希望JSON解析器值使用intent.putExtra另一个活动。

我不知道

Intent intent = new Intent(this, anotheractivity.class);
 intent.putExtra("information","I don't know here");
 startActivity(intent);

我想要school.job数据另一个活动传输。

如何使用数据传输JSON解析器数据?

3 个答案:

答案 0 :(得分:0)

将Json解析在同一结构的对象中,然后使用intent

传递该对象
University uni = new University();
uni.setTitle(jsonObj.getJSONObject("Title").opt("val").toString());
Intent i = new Intent(this, anotheractivity.class);
i.putExtra("information", uni);

然后你可以在其他活动中从意图中获取你的对象

答案 1 :(得分:0)

在另一项活动中获取数据的简单方法是

将JSon字符串放入putExtra() 然后获取字符串,将其解析为另一个活动中的JSON对象。

答案 2 :(得分:0)

As per your code:

    @Override 
    protected void onPostExecute(String result) {
       super.onPostExecute(result);

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
    try { 
        JSONArray arr =new JSONArray(result);
           JSONObject ob = arr.getJSONObject(0);       
           strData += ob.getString("name") + "\n"
                   + " - " + ob.getString("school") + "\n"
                   + " - " + ob.getString("job") + "\n\n";
           adapter.add(strData);
    } catch (JSONException e) {
       Log.d(TAG,"DD");
    }  
    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);
    } 

    you are getting server response in result String, so just put the "result" in Intent as follows:

    Intent intent = new Intent(this, anotheractivity.class);
     intent.putExtra("result","result");
     startActivity(intent);

    In anotheractivity.class onCreate method:
    String result=getIntent().getStringExtra("result");
    Where result contain your JSON data.