I am using the following code for parsing 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()) !=开发者_Go百科 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;
}
}
But I'm getting the following exception -
TypeNotFoundException
I am using the gson library for parsing json.
OS : Android 2.2
Hardware : HTC Desire Z
I found this problem reported here -
http://code.google.com/p/android/issues/detail?id=1760
and I found the solution to this problem here -
http://code.google.com/p/google-gson/issues/detail?id=255
Basically, I followed these steps -
If anyone else has this problem, here's a quick how-to fix it:
- Download jarjar (http://code.google.com/p/jarjar/downloads/list)
- Put jarjar-1.0.jar and gson-1.5.jar in the same folder
- Create a new text file in this folder (rules.txt)
- Write the following line in the textfile: rule com.google.gson.** com.google.myjson.@1
- From the commandline, open jarjar with the command "java -jar jarjar.jar process rules.txt gson-1.5.jar myjson-1.5.jar"
- Replace the gson library in your project with myjson and update the imports
and that solved the issue for me.
精彩评论