我使用以下代码解析JSON -
/**
* ScoreReader.java
*/
package ca.cbc.mobile.android.model;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import ca.cbc.mobile.android.model.HNICBoxScore;
import android.os.Environment;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gjson.reflect.TypeToken;
public class BoxScoreReader
{
// static Logger logger = Logger.getLogger(HNICBoxScoreReader.class);
private static final String TAG = "HNICCompletedBoxScoreReader";
List<HNICBoxScore> boxScoreList = null;
public List<HNICBoxScore> readBoxScores(String jsonFile)
{
String json = null;
try
{
URL url = new URL(jsonFile);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
StringBuffer buffer = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null)
buffer.append(inputLine);
json = buffer.toString();
Gson gson = new Gson();
Type listType = new TypeToken<List<HNICBoxScore>>()
{
}.getType();
boxScoreList = gson.fromJson(json, listType);
if (boxScoreList != null)
{
for (HNICBoxScore boxScore : boxScoreList)
{
// logger.debug(boxScore);
Log.d(TAG, "----------------------" + boxScore.getAway());
}
}
else
{
Log.d(TAG, "--------------------------------problems reading completed box score");
}
} catch (Exception e)
{
// logger.error(e);
Log.e(TAG, "---------------------problems reading completed box score" + e.toString());
}
return boxScoreList;
}
}
但我得到以下异常 -
TypeNotFoundException
我正在使用gson库来解析json。
操作系统:Android 2.2
硬件:HTC Desire Z
答案 0 :(得分:3)
我在这里报告了这个问题 -
http://code.google.com/p/android/issues/detail?id=1760
我在这里找到了解决这个问题的方法 -
http://code.google.com/p/google-gson/issues/detail?id=255
基本上,我遵循了这些步骤 -
如果有其他人遇到此问题,请按以下步骤快速解决问题:
这解决了我的问题。