我尝试使用GSON库解析JSON。参考。我使用的链接:link
json文件:
{"Details": [{"EndDate": "01/11/2013 05:10:00","Type": "Numeric", "StartDate": "12/12/2012 04:00:00", "UTCDate": "12/31/2012 07:55:01", "Min": "1000.00", "ConvertedStartDate": "12/12/2012 04:00:00", "ConvertedUTCDate": "12/31/2012 07:55:01", "Name": "User Car Auction", "Max": "3000.00", "ConvertedEndDate": "01/11/2013 05:10:00" }], "result": [{ "AllowtoComment": false, "Rank": 1, "Low": "5000.00", "DetailsID": 51, "ItemDetails": {"ID": 28, "SpecificationFile": "", "ItemDescription": "Pristine White accord. 176K miles. Runs Great", "ImageName": "http://64.151.125.199/Auction/28/Desert.jpg", "ItemName": "1986 Honda Accord"}, "Desc": "Pristine White accord. 176K miles. Runs Great", "Comment": "<null>", "DetailsID": 78, "Name": "1986 Honda Accord", "LowRank": 1 }, { "AllowtoComment": false, "Rank": 1, "Low": "7000.00", "DetailsID": 52, "ItemDetails": {"ID": 28, "SpecificationFile": "", "ItemDescription": "Black w/Tan Interior. 110K Miles. New tires", "ImageName": "http://64.151.125.199/Auction/28/Penguins.jpg", "ItemName": "1989 Isuzu Trooper"}, "Desc": "Black w/Tan Interior. 110K Miles. New tires", "Comment": "N/A", "DetailsID": 100, "Name": "1989 Isuzu Trooper", "LowRank": 1 }]}
ItemDetails.java
public class ItemDetails {
public int ID;
public String SpecificationFile;
public String ItemDescription;
public String ImageName;
public String ItemName;
}
Result.java
public class Result {
public boolean AllowtoComment;
public int Rank;
public String Low;
public int DetailsID;
public boolean ShowCompetingBid;
public ItemDetails itemDetails;
public String Desc;
public String Comment;
public int DetailsID;
public String Name;
public int LowRank;
}
Detail.java
public class Detail {
public String EndDate;
public String Type;
public String StartDate;
public String UTCDate;
public String Min;
public String ConvertedStartDate;
public String ConvertedUTCDate;
public String Name;
public String Max;
public String ConvertedEndDate;
}
Responce.java
public class Responce {
public List<Detail> details;
public List<Result> results;
}
主要Java代码
public class GsonParsingActivity extends Activity {
private ProgressBar bgProcess;
private String SERVER = "url";
private String TAG = "LargeJSON";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gson);
bgProcess = (ProgressBar) findViewById(R.id.progressBarGson);
new GetDetail().execute();
}
private class GetDetail extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
bgProcess.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
Map<String, String> mMap = new HashMap<String, String>();
mMap.put("ID", "28");
mMap.put("ParticipantID", "2");
mMap.put("CompanyID", "5");
InputStream source = getDataForSimpleMap(SERVER + "getList",
mMap);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
AuctionResponce responce = gson.fromJson(reader,
Responce.class);
List<Result> results = responce.results;
Log.d(TAG, ">>>>>>>>>>>>>> " + results.size());
for (Result result : results) {
Log.v(TAG, " ==================> "+ result.BidDetailsID);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
bgProcess.setVisibility(View.INVISIBLE);
}
}
@SuppressWarnings("rawtypes")
private InputStream getDataForSimpleMap(String strURL,
Map<String, String> params) {
try {
// JSONObject jArray = null;
InputStream stream = null;
Iterator iterator = params.entrySet().iterator();
JSONObject data = new JSONObject();
while (iterator.hasNext()) {
Map.Entry mEntry = (Map.Entry) iterator.next();
String key = mEntry.getKey().toString();
String value = mEntry.getValue().toString();
data.put(key, value);
}
stream = getJSONFromURL(strURL, data);
return stream;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private InputStream getJSONFromURL(String strURL, JSONObject data) {
try {
InputStream stream = null;
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
120000);
HttpPost httpPost = new HttpPost(strURL);
httpPost.setHeader("content-type", "application/json");
if (data != null) {
StringEntity entity = new StringEntity(data.toString(),
HTTP.UTF_8);
httpPost.setEntity(entity);
}
HttpResponse response = null;
response = httpClient.execute(httpPost);
if (response != null) {
stream = response.getEntity().getContent();
} else {
stream = null;
}
return stream;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
但不幸的是我收到了以下错误:
02-11 12:10:01.075: W/System.err(587): java.lang.NullPointerException
02-11 12:10:01.094: W/System.err(587): at com.example.largejson.GsonParsingActivity$GetAuctionDetail.doInBackground(GsonParsingActivity.java:80)
02-11 12:10:01.094: W/System.err(587): at com.example.largejson.GsonParsingActivity$GetAuctionDetail.doInBackground(GsonParsingActivity.java:1)
02-11 12:10:01.094: W/System.err(587): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-11 12:10:01.094: W/System.err(587): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-11 12:10:01.105: W/System.err(587): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-11 12:10:01.105: W/System.err(587): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-11 12:10:01.115: W/System.err(587): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-11 12:10:01.115: W/System.err(587): at java.lang.Thread.run(Thread.java:1019)