如何在下一个活动中显示响应

在我的应用程序中,我正在搜索模块我之前的问题是如何在其他活动中显示json响应? 因为我在服务器发送请求,然后我得到响应,作为回应我得到一些数据,我希望在下一页显示的数据,我不知道该怎么做,任何人都可以帮助吗?

class AttemptLogin extends AsyncTask { boolean failure = false; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(getActivity()); pDialog.setMessage("Processing.."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @SuppressWarnings("unused") @Override protected String doInBackground(String...args) { //Check for success tag //int success; Looper.prepare(); String userids = strtext.toString(); String contri=spcountry.getText().toString(); String states=spstate.getText().toString(); String city=spcity.getText().toString(); System.out.println("Email : " + userids); System.out.println("Email : " + agesfrom); System.out.println("Days : " + agesto); System.out.println("Months : " + heightfroms); System.out.println("Years : " + heighttos); System.out.println("User : " + language); System.out.println("Password : " + religion); System.out.println("Gender : " + marriage); System.out.println("First NM : " + contri); System.out.println("Last NM : " + states); System.out.println("Profile : " + city);*/ try { //Building Parameters List params = new ArrayList(); params.add(new BasicNameValuePair("user_login_id", userids)); /* params.add(new BasicNameValuePair("age_from", agesfrom)); params.add(new BasicNameValuePair("age_to", agesto)); params.add(new BasicNameValuePair("height_from", heightfroms)); params.add(new BasicNameValuePair("height_to", heighttos)); params.add(new BasicNameValuePair("language", language)); params.add(new BasicNameValuePair("religion", religion)); params.add(new BasicNameValuePair("maritalstatus", marriage)); params.add(new BasicNameValuePair("country", contri)); params.add(new BasicNameValuePair("state", states)); params.add(new BasicNameValuePair("city", city)); */ params.add(new BasicNameValuePair("version", "apps")); Log.d("request!", "starting"); // getting product details by making HTTP request json = jsonParser.makeHttpRequest ( SEARCH_URL, "POST", params); //check your log for json response Log.d("Request attempt", json.toString()); final String str = json.toString(); JSONObject jobj = new JSONObject(json.toString()); final String msg = jobj.getString("searchresult"); return json.getString(TAG_SUCCESS); }catch (JSONException e) { e.printStackTrace(); } return null; } // After completing background task Dismiss the progress dialog protected void onPostExecute(String file_url) { //dismiss the dialog once product deleted pDialog.dismiss(); Intent intent=new Intent(getActivity(),SearchResults.class); intent.putExtra("id", strtext); intent.putExtra("whole", json.getString(TAG_SUCCESS)); startActivity(intent); }} 

Searchresult.java

  Id=this.getIntent().getStringExtra("id"); System.out.println("searching id"+Id); results=this.getIntent().getStringExtra("whole"); System.out.println("Results"+results); nomathc=(TextView)findViewById(R.id.no_match); 

在下一个活动中显示响应可以通过以下方式完成

解决方案1响应来自字符串,因此通过使用将整个响应传递给下一个活

  intent.putExtras("Key",response); 

解决方案2制作一个单独的getter setter类,通过使用这个类,你可以在我们的应用程序中设置和获取值。

喜欢这个

步骤1

 public class SingleObject { //create an object of SingleObject private static SingleObject instance = new SingleObject(); private String name; private String age; //make the constructor private so that this class cannot be //instantiated private SingleObject(){} //Get the only object available public static SingleObject getInstance(){ return instance; } // here u can declare getter setter public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } } 

第2步在第一个活动中使用它

  Singleton tmp = Singleton.getInstance( ); tmp.setName(""); 

第3步

在这样的下一个活动中

  Singleton tmp = Singleton.getInstance( ); String name = tmp.getName; 

解决方案3 :创建一个静态的hashmap Arraylist并在任何活动中使用它

或者可能有更多解决方案…..

据我所知,您需要知道的是如何将JSON结果绑定到列表视图。 如果这就是你的意思,请继续阅读……

我希望你的XML视图中已有一个listview用于SearchResults活动。 如果没有添加一个(比如R.id.listView1 )。

然后创建一个row_listitem.xml文件,以格式化列表中的一个项目的外观。

      ...   

现在您需要创建一个自定义适配器(比如ResultAdapter )。

 public class ResultAdapter extends BaseAdapter { private JSONArray jsonArray; private LayoutInflater inflater = null; public ResultAdapter(Activity a, JSONArray b) { jsonArray = b; inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { return title.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; if (convertView == null) vi = inflater.inflate(R.layout.row_listitem, null); try { JSONObject jsonObject = jsonArray.getJSONObject(position); TextView name = (TextView) vi.findViewById(R.id.item_name); String nameText = jsonObject.getString("name").toString(); name.setText(nameText); TextView location = (TextView) vi.findViewById(R.id.item_location); String locationText = jsonObject.getString("location").toString(); location.setText(locationText); ... } catch (JSONException e) { e.printStackTrace(); } return vi; } } 

然后,您可以修改您的SearchResults活动。

 public class SearchResults extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_searchresults); ListView list = (ListView) findViewById(R.id.listView1); String results = this.getIntent().getStringExtra("whole"); JSONArray jsonArray = new JSONArray(results); ResultAdapter adapter = new ResultAdapter(SearchResults.this, jsonArray); list.setAdapter(adapter); } ... } 

不检查代码是否存在编译错误。 但是我猜你会得到漂移。 干杯!

将对象存储在需要两个参数(对象和唯一键)的静态变量中。

 // This class will hold the OBJECT containing a key in order to Access the //location in our Custom Stack class Holder{ private Object obj = null; private String key = null; public Holder(Object obj, String key){ this.obj = obj; this.key = key; } } // This will serve as the storage for your JSON value class GlobalStorage{ public static ArrayList stack = new ArrayList(); public static Object getValue(String key){ for(int x = 0 ; x < stack.size() ; x++){ if(stack.get(x).equals(key)){ return stack.get(x); } } return null; } } //In your OnPost protected void onPostExecute(String file_url){ // your own code here .. GlobalStorage.add(new Holder("HI THIS IS MY JASON RESPONSE","whole")); // your own code here.. } 

Searchresult.java

  // myJson holds the value String myJson = (String)GlobalStorage.getValue("whole"); 

使用bundle将您的响应从一个活动移动到另一个活动

-create实现Parcelable的类

  public class ResponseData implements Parcelable{} 

– 接下来将其保存在意图中:

 intent.putExtra(key, new ResponseData(someDataFromServer)); 

-step – 回复它:

  Bundle data = getIntent().getExtras(); ResponseData response= Response response =(ResponseData ) data.getParcelable(key); 

– 显示它:

  textView.setText(data from response); 

之后,将其添加到适配器以显示给用户

在其他情况下,您可以将其保存在应用程序上下文或数据库中(不推荐)

广播通知:

 protected void onPostExecute(String file_url) { //dismiss the dialog once product deleted pDialog.dismiss(); Intent intent=new Intent(getActivity(),SearchResults.class); sendNotificationResponse(json.getString(TAG_SUCCESS)); startActivity(intent); } private void sendNotificationResponse(String response) { Intent intent = new Intent("KEY_INTENT"); intent.putExtra("KEY_RESPONSE", response.toString()); LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent); } 

然后在SearchResult.java中:

 // handler for received intent private BroadcastReceiver responseReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { String response = extras.getString("KEY_RESPONSE"); //If you want it back as a JSON object JSONObject object = new JSONObject(response); //DO SOMETHING WITH THIS RESPONSE HERE } } }; @Override public void onResume() { super.onResume(); LocalBroadcastManager.getInstance(this).registerReceiver(responseReceiver, new IntentFilter("KEY_INTENT")); } @Override public void onDestroy() { super.onDestroy(); // Unregister since the activity is not visible LocalBroadcastManager.getInstance(this).unregisterReceiver(responseReceiver); } 

我相信应该这样做。

我只是添加了hashmap并将其称为下一个活动,它的工作正常,

  HashMap hmp = new HashMap(); hmp.put(TAG_AGE, Ages+" years"); hmp.put(TAG_CAST, Casts); hmp.put(TAG_IMAGE, Images); hmp.put(TAG_LOCATION, Locations); hmp.put(TAG_MATCH_ID, match_Detail_id); hmp.put(TAG_NAME, Names); hmp.put(TAG_PROFILE, Profiles); alhmp.add(hmp); 

在下一个活动中

  Intent intent = getIntent(); aList = (ArrayList>) intent.getSerializableExtra("match_data"); adapter = new CustomAdapterSearch(SearchResults.this, aList); setListAdapter(adapter);