我的问题如下我正在开发一个应用程序,它正在将 JSON 对象数组读入字符串数组列表,在获取数据后应该在列表适配器中使用它似乎正在发生的事情是当我尝试将 ListView 适配器设置为创建它的简单适配器因标题中的错误而爆炸,即错误转换结果 java.io.IOException: Attempted read on closed stream。任何帮助将不胜感激我在下面附上了相关课程
package com.jbsoft.android;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.jbsoft.library.JSONParser;
import com.jbsoft.library.UserFunctions;
public class Policies extends ActivityGroup{
protected static LocalActivityManager mLocalActivityManager;
;
protected static ArrayList<String> policy_list= new ArrayList<String>(); // Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
UserFunctions uf = new UserFunctions();
JSONArray policies = null;
// products JSONArray
JSONArray policylist = null;
ArrayList<HashMap<String, String>> policies_List = new ArrayList<HashMap<String, String>>();
// url to make request
private static String policiesURL = "http://api.agentpitstop.com/mobile/policies.php?action=status&start=2010-01-01&end=2013-12-31";
// JSON Node names
private static final String TAG_POLICIES = "policies";
private static final String TAG_FIRST = "firstname";
private static final String TAG_LAST = "lastname";
private static final String TAG_PRODUCERID = "producerid";
private static final String TAG_COMPANY = "company";
private static final String TAG_PLAN = "plan";
private static final String TAG_POLICYID = "policyid";
private static final String TAG_POLICYNUMBER = "policynumber";
private static final String TAG_STATUS = "status";
private static final String TAG_SUBMITTED= "submitted";
private static final String TAG_EFFECTIVE= "effective";
private static final String TAG_ADDED= "added";
private static final String TAG_EMAIL= "email";
private static final String TAG_PHONE= "phone";
private static final String TAG_CHANGED= "changed";
private static final String TAG_TERMINATION= "termination";
private static final String TAG_GUID= "guid";
private static final String TAG_CLIENTID= "clientid";
private static final String TAG_POLICYTYPE= "policytype";
private static final String TAG_POLICYPRODUCER= "policyproducer";
private static final String TAG_PRIMARYAGENT= "primaryagent";
// contacts JSONArray
protected ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.policies_list);
new LoadPolicies().execute();
}
/**
* Background Async Task to Load agents policies submissions
* */
class LoadPolicies extends AsyncTask<String, String, String> {
ArrayList<HashMap<String, String>> policies_List = new ArrayList<HashMap<String, String>>();
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Policies.this);
pDialog.setMessage("Loading Submitted Policies");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
/**
* getting policy JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// Hashmap for ListView
// getting JSON string from URL
GlobalVariable apploginurl = ((GlobalVariable)getApplicationContext());
apploginurl.getState();
String startdate = apploginurl.getStartDate();
String enddate = apploginurl.getEndDate();
@SuppressWarnings("unused")
JSONObject json1 = jsonParser.getJSONFromUrl(apploginurl.saveurl, params);
JSONObject json2 = uf.getSubmissions(startdate, enddate);
// Check your log cat for JSON response // Hashmap for ListView
try {
policies = json2.getJSONArray(TAG_POLICIES);
// looping through All messages
for (int i = 0; i < policies.length(); i++) {
JSONObject c = policies.getJSONObject(i);
// Storing each json item in variable
String firstname = c.getString(TAG_FIRST);
String last = c.getString(TAG_LAST);
String producerid = c.getString(TAG_PRODUCERID);
String company = c.getString(TAG_COMPANY);
String plan = c.getString(TAG_PLAN);
String policyid = c.getString(TAG_POLICYID);
String policynumber = c.getString(TAG_POLICYNUMBER);
String status = c.getString(TAG_STATUS);
String submitted = c.getString(TAG_SUBMITTED);
String effective = c.getString( TAG_EFFECTIVE);
String added = c.getString(TAG_ADDED);
// String changed = c.getString(TAG_CHANGED);
String termination = c.getString(TAG_TERMINATION);
String guid = c.getString(TAG_GUID);
String clientid = c.getString(TAG_CLIENTID);
String policytype = c.getString(TAG_POLICYTYPE);
String policyproducer = c.getString(TAG_POLICYPRODUCER);
String primaryagent = c.getString( TAG_PRIMARYAGENT);
String phone = c.getString(TAG_PHONE);
String email = c.getString( TAG_EMAIL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_FIRST, firstname);
map.put(TAG_LAST, last);
map.put(TAG_COMPANY,company);
map.put(TAG_PRODUCERID, producerid);
map.put(TAG_PLAN, plan);
map.put(TAG_POLICYID,policyid);
map.put(TAG_POLICYNUMBER, policynumber);
map.put(TAG_STATUS, status);
map.put(TAG_SUBMITTED,submitted);
map.put(TAG_EFFECTIVE, effective);
map.put(TAG_ADDED, added);
map.put(TAG_TERMINATION,termination);
map.put(TAG_GUID, guid);
map.put(TAG_CLIENTID, clientid);
map.put(TAG_POLICYTYPE, policytype);
map.put(TAG_PRIMARYAGENT, primaryagent);
map.put(TAG_POLICYPRODUCER, policyproducer);
map.put(TAG_PHONE, phone);
map.put(TAG_EMAIL, email);
// adding HashList to ArrayList
policies_List.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
Policies.this, policies_List,
R.layout.policies_list_item, new String[] { TAG_FIRST, TAG_LAST,TAG_COMPANY},
new int[] {R.id.first,R.id.last, R.id.company});
lv.setAdapter(adapter);
lv.getAdapter();
// updating UI from Background Thread
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent in = new Intent(view.getContext(), SingleMenuItemActivity.class);
//Activity1 parentActivity = (Activity1)getParent();
replaceContentView("policies_list_item_detail", in);
// getting values from selected ListItem
String first = ((TextView) view.findViewById(R.id.first)).getText().toString();
String last = ((TextView) view.findViewById(R.id.last)).getText().toString();
String company = ((TextView) view.findViewById(R.id.company)).getText().toString();
@SuppressWarnings("unused")
String plan = policies_List.get(position).get(TAG_PLAN);
// Starting new intent
in.putExtra(TAG_FIRST, first);
in.putExtra(TAG_LAST, last);
in.putExtra(TAG_COMPANY, company);
in.putExtra(TAG_PLAN, policies_List.get(position).get(TAG_PLAN));
in.putExtra(TAG_SUBMITTED, policies_List.get(position).get(TAG_SUBMITTED));
in.putExtra(TAG_STATUS, policies_List.get(position).get(TAG_STATUS));
in.putExtra(TAG_EFFECTIVE, policies_List.get(position).get(TAG_EFFECTIVE));
in.putExtra(TAG_PHONE, policies_List.get(position).get(TAG_PHONE));
in.putExtra(TAG_EMAIL, policies_List.get(position).get(TAG_EMAIL));
startActivity(in);
}
});
}});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); setContentView(view);
} }
这是我的日志......
08-15 19:06:50.166: W/ActivityThread(15532): Application com.jbsoft.android is waiting for the debugger on port 8100...
08-15 19:06:50.170: I/System.out(15532): Sending WAIT chunk
08-15 19:06:50.182: I/dalvikvm(15532): Debugger is active
08-15 19:06:50.369: I/System.out(15532): Debugger has connected
08-15 19:06:50.369: I/System.out(15532): waiting for debugger to settle...
08-15 19:06:50.569: I/System.out(15532): waiting for debugger to settle...
08-15 19:06:50.772: I/System.out(15532): waiting for debugger to settle...
08-15 19:06:50.971: I/System.out(15532): waiting for debugger to settle...
08-15 19:06:51.186: I/System.out(15532): waiting for debugger to settle...
08-15 19:06:51.389: I/System.out(15532): waiting for debugger to settle...
08-15 19:06:51.588: I/System.out(15532): waiting for debugger to settle...
08-15 19:06:51.791: I/System.out(15532): debugger has settled (1353)
08-15 19:06:52.408: I/global(15532): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
08-15 19:06:52.588: D/dalvikvm(15532): GC_EXTERNAL_ALLOC freed 964 objects / 90272 bytes in 87ms
08-15 19:06:52.686: I/System.out(15532): Could not parse java.lang.NumberFormatException: unable to parse 'null' as integer
08-15 19:06:52.866: W/ResourceType(15532): Skipping entry 0x7f05000a in package table 0 because it is not complex!
08-15 19:06:52.869: W/ResourceType(15532): Skipping entry 0x7f05000a in package table 0 because it is not complex!
08-15 19:06:52.877: W/ResourceType(15532): Skipping entry 0x7f05000a in package table 0 because it is not complex!
08-15 19:06:52.877: W/ResourceType(15532): Skipping entry 0x7f05000a in package table 0 because it is not complex!
08-15 19:06:52.881: W/ResourceType(15532): Skipping entry 0x7f05000a in package table 0 because it is not complex!
08-15 19:06:53.158: D/skia(15532): purging 6K from font cache [1 entries]
08-15 19:06:53.158: D/SKIA_FONT(15532): stored typeface in slot 0
08-15 19:06:53.170: D/SKIA_FONT(15532): SKIA_FONT fontfile: /data/data/com.android.settings/app_fonts/default/DroidSans.ttf
08-15 19:06:53.182: D/SKIA_FONT(15532): SKIA_FONT fontfile: /data/data/com.android.settings/app_fonts/default/DroidSans.ttf
08-15 19:06:53.182: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Sans, style = 0, uniqueID = 13
08-15 19:06:53.182: D/SKIA_FONT(15532): SKIA_FONT fontfile: /data/data/com.android.settings/app_fonts/default/DroidSans-Bold.ttf
08-15 19:06:53.213: D/SKIA_FONT(15532): SKIA_FONT fontfile: /data/data/com.android.settings/app_fonts/default/DroidSans-Bold.ttf
08-15 19:06:53.213: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Sans, style = 1, uniqueID = 14
08-15 19:06:53.260: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Serif, style = 0, uniqueID = 15
08-15 19:06:53.276: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Serif, style = 1, uniqueID = 16
08-15 19:06:53.291: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Serif, style = 2, uniqueID = 17
08-15 19:06:53.307: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Serif, style = 3, uniqueID = 18
08-15 19:06:53.319: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Sans Mono, style = 0, uniqueID = 19
08-15 19:06:53.323: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Sans Arabic, style = 0, uniqueID = 20
08-15 19:06:53.330: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Sans Hebrew, style = 0, uniqueID = 21
08-15 19:06:53.366: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Sans Thai, style = 0, uniqueID = 22
08-15 19:06:53.405: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Sans, style = 0, uniqueID = 23
08-15 19:06:53.491: D/SKIA_FONT(15532): load_system_fonts(), name = Droid Sans Fallback, style = 0, uniqueID = 24
08-15 19:06:53.491: D/SKIA_FONT(15532): load_system_fonts(), oldSansUID = 1, newSansUID = 13
08-15 19:06:53.491: D/SKIA_FONT(15532): load_system_fonts(), oldSansBoldUID = 2, newSansBoldUID = 14
08-15 19:06:58.151: W/IInputConnectionWrapper(15532): showStatusIcon on inactive InputConnection
08-15 19:07:11.537: D/dalvikvm(15532): GC_FOR_MALLOC freed 1616 objects / 94344 bytes in 34ms
08-15 19:07:19.143: I/System.out(15532): executing request http://<domain>/mobile/authenticate.php?action=login&username=<user>&password=<password>
08-15 19:07:21.284: D/dalvikvm(15532): GC_FOR_MALLOC freed 2134 objects / 118480 bytes in 45ms
08-15 19:07:24.752: I/System.out(15532): executing request http://<domain>/mobile/authenticate.php?action=login&username=<user>&password=<password>
08-15 19:07:26.635: E/JSON(15532): {"success":true,"message":"Successfully logged in.","uid":"3401"}
08-15 19:07:27.518: D/dalvikvm(15532): GC_FOR_MALLOC freed 1590 objects / 80792 bytes in 36ms
08-15 19:07:30.940: E/Buffer Error(15532): Error converting result java.io.IOException: Attempted read on closed stream.
08-15 19:07:32.299: I/System.out(15532): executing request http://<domain>/mobile/policies.php?action=status&start=2010-01-01&end=2013-12-31
08-15 19:07:32.405: I/System.out(15532): executing request http://<domain>/mobile/policies.php?action=status&start=2010-01-01&end=2013-12-31
08-15 19:07:32.592: D/dalvikvm(15532): GC_FOR_MALLOC freed 2396 objects / 115488 bytes in 37ms
08-15 19:07:33.252: D/dalvikvm(15532): GC_FOR_MALLOC freed 137 objects / 188984 bytes in 42ms
08-15 19:07:34.315: I/JSON(15532): {"success":true,"policies":[{"firstname":"test","lastname":"test","producerid":"10115","company":"Anthem Blue Cross","plan":"Anthem PPO 2013","policyid":"50864","policynumber":"12345678","status":"Active","submitted":"Jul 20 2013 12:00:00:000AM","effective":"Aug 1 2013 12:00:00:000AM","added":"Sep 25 2012 04:19:45:163PM","changed":"Jul 19 2013 10:56:36:633AM","termination":"","guid":"38286","email":"ernesto@vanbergins.com","phone":"9253030957","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"38286","policytype":"Medicare Advantage","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"John","lastname":"Doe","producerid":"10115","company":"AARP","plan":"AARP Medicare RX Preffered","policyid":"36291","policynumber":"PENDING","status":"Cancelled","submitted":"May 30 2012 11:44:31:927AM","effective":"Jul 1 2012 11:44:35:277AM","added":"Sep 15 2011 11:44:41:640AM","changed":"May 30 2012 09:50:32:770AM","termination":"","guid":"33601","email":"","phone":"9256345702","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"33601","policytype":"Medicare Part D","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"John","lastname":"Doe","producerid":"10115","company":"Anthem Blue Cross","plan":"Plan F","policyid":"36289","policynumber":"012A3456","status":"Active","submitted":"May 30 2012 11:43:12:450AM","effective":"Jul 1 2012 11:43:17:340AM","added":"Sep 15 2011 11:43:30:203AM","changed":"May 30 2012 09:57:08:943AM","termination":"","guid":"33601","email":"","phone":"9256345702","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"33601","policytype":"Medicare Supplement","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"test","lastname":"test","producerid":"10115","company":"Z NIU * WellCare","plan":"Z NIU * WellCare Sonata","policyid":"46700","policynumber":"CANCELLED","status":"Cancelled","submitted":"May 19 2012 12:00:00:000AM","effective":"Jul 1 2012 12:00:00:000AM","added":"Feb 6 2012 08:24:17:977PM","changed":"Jul 19 2013 10:56:47:497AM","termination":"Jan 1 1990 12:00:00:000AM","guid":"38286","email":"ernesto@vanbergins.com","phone":"9253030957","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"38286","policytype":"Medicare Advantage","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"John","lastname":"Doe","producerid":"10115","company":"Humana","plan":"Humana Walmart-Preferred RX","policyid":"34417","policynumber":"PENDING","status":"Pending","submitted":"Apr 15 2011 11:36:32:680AM","effective":"May 1 2011 11:36:32:683AM","added":"Apr 15 2011 11:36:46:153AM","changed":"Apr 15 2011 11:37:11:950AM","termination":"Apr 15 2011 11:37:09:227AM","guid":"33602","email":"ernesto@agentpitstop.com","phone":"9254186012","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"33602","policytype":"Medicare Part D","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"John","lastname":"Doe","producerid":"10115","company":"Anthem Blue Cross","plan":"Anthem","policyid":"34413","policynumber":"321A98765","status":"Active","submitted":"Apr 15 2011 11:32:42:540AM","effective":"May 1 2011 11:32:42:543AM","added":"Apr 15 2011 11:33:18:183AM","changed":"Apr 15 2011 11:37:22:860AM","termination":"Apr 15 2011 11:37:20:990AM","guid":"33602","email":"ernesto@agentpitstop.com","phone":"9254186012","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"33602","policytype":"Medicare Advantage","policyproducer":"Test, Guest","primaryagent":"Test, Guest"}]}
08-15 19:07:34.319: E/JSON(15532): {"success":true,"policies":[{"firstname":"test","lastname":"test","producerid":"10115","company":"Anthem Blue Cross","plan":"Anthem PPO 2013","policyid":"50864","policynumber":"12345678","status":"Active","submitted":"Jul 20 2013 12:00:00:000AM","effective":"Aug 1 2013 12:00:00:000AM","added":"Sep 25 2012 04:19:45:163PM","changed":"Jul 19 2013 10:56:36:633AM","termination":"","guid":"38286","email":"ernesto@vanbergins.com","phone":"9253030957","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"38286","policytype":"Medicare Advantage","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"John","lastname":"Doe","producerid":"10115","company":"AARP","plan":"AARP Medicare RX Preffered","policyid":"36291","policynumber":"PENDING","status":"Cancelled","submitted":"May 30 2012 11:44:31:927AM","effective":"Jul 1 2012 11:44:35:277AM","added":"Sep 15 2011 11:44:41:640AM","changed":"May 30 2012 09:50:32:770AM","termination":"","guid":"33601","email":"","phone":"9256345702","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"33601","policytype":"Medicare Part D","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"John","lastname":"Doe","producerid":"10115","company":"Anthem Blue Cross","plan":"Plan F","policyid":"36289","policynumber":"012A3456","status":"Active","submitted":"May 30 2012 11:43:12:450AM","effective":"Jul 1 2012 11:43:17:340AM","added":"Sep 15 2011 11:43:30:203AM","changed":"May 30 2012 09:57:08:943AM","termination":"","guid":"33601","email":"","phone":"9256345702","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"33601","policytype":"Medicare Supplement","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"test","lastname":"test","producerid":"10115","company":"Z NIU * WellCare","plan":"Z NIU * WellCare Sonata","policyid":"46700","policynumber":"CANCELLED","status":"Cancelled","submitted":"May 19 2012 12:00:00:000AM","effective":"Jul 1 2012 12:00:00:000AM","added":"Feb 6 2012 08:24:17:977PM","changed":"Jul 19 2013 10:56:47:497AM","termination":"Jan 1 1990 12:00:00:000AM","guid":"38286","email":"ernesto@vanbergins.com","phone":"9253030957","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"38286","policytype":"Medicare Advantage","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"John","lastname":"Doe","producerid":"10115","company":"Humana","plan":"Humana Walmart-Preferred RX","policyid":"34417","policynumber":"PENDING","status":"Pending","submitted":"Apr 15 2011 11:36:32:680AM","effective":"May 1 2011 11:36:32:683AM","added":"Apr 15 2011 11:36:46:153AM","changed":"Apr 15 2011 11:37:11:950AM","termination":"Apr 15 2011 11:37:09:227AM","guid":"33602","email":"ernesto@agentpitstop.com","phone":"9254186012","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"33602","policytype":"Medicare Part D","policyproducer":"Test, Guest","primaryagent":"Test, Guest"},{"firstname":"John","lastname":"Doe","producerid":"10115","company":"Anthem Blue Cross","plan":"Anthem","policyid":"34413","policynumber":"321A98765","status":"Active","submitted":"Apr 15 2011 11:32:42:540AM","effective":"May 1 2011 11:32:42:543AM","added":"Apr 15 2011 11:33:18:183AM","changed":"Apr 15 2011 11:37:22:860AM","termination":"Apr 15 2011 11:37:20:990AM","guid":"33602","email":"ernesto@agentpitstop.com","phone":"9254186012","birthday":"","address":"","city":"","state":"","zip":"","bflagphone":"1","clientid":"33602","policytype":"Medicare Advantage","policyproducer":"Test, Guest","primaryagent":"Test, Guest"}]}
**08-15 19:07:34.565: E/Buffer Error(15532): Error converting result java.io.IOException: Attempted read on closed stream.**
08-15 19:07:43.698: D/dalvikvm(15532): threadid=9: still suspended after undo (sc=1 dc=1 s=Y)
08-15 19:07:46.010: D/dalvikvm(15532): threadid=9: still suspended after undo (sc=1 dc=1 s=Y)
08-15 19:08:07.498: D/PhoneWindow(15532): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@47c7ddc8 has no id.