简体   繁体   English

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

[英]How to use Intent putExtra JSON data on android

Recently, in my web server, to get JSON text, I'm using AsyncTask 最近,在我的Web服务器上,为了获取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);
}

result 结果

aaa
- seoul university
- teacher

and I want JSON parser value use intent.putExtra another activity. 并且我想要JSON解析器值使用intent.putExtra另一个活动。

I don't know 我不知道

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

I want school.job data another activity transport. 我想要school.job数据进行其他活动传输。

how to use data transport JSON parser data? 如何使用数据传输JSON解析器数据?

Parse your Json inside an object of the same structure, then pass that object with the intent 在相同结构的对象中解析您的Json,然后将其意图传递给该对象

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

then you can get your object from the Intent in your other activity 然后您可以在其他活动中从Intent中获取对象

A simple way to get the data in another activity is 在另一个活动中获取数据的一种简单方法是

To put JSon string in putExtra() and then get the string, parsing it into JSON objects in another activity. 将JSon字符串放入putExtra() ,然后获取该字符串,然后将其解析为另一个活动中的JSON对象。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM