我想使用 Gson 来解析我的 JSON,我已将 Gson 库添加到我的项目中。当我编译它时一切都很好,但是当我运行它时,我收到错误消息日志说
Could not find class 'com.google.gson.Gson', referenced from method report.weeklyflash.WeeklyFlashIdActivity.onCreate
这是使用 Gson 的代码:
import com.google.gson.Gson;
import report.weeklyflash.ReportResult;
import report.weeklyflash.ReportResults;
public class WeeklyFlashIdActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
System.out.println("oncreate");
final TableLayout tableLayout = (TableLayout) findViewById(R.id.headerTable);
InputStream is = null;
String json = "";
//http get content
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://10.80.3.73/webservice/Service1.svc/json/weeklyflash/my");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
json=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}
ReportResults reports = new Gson().fromJson(json, ReportResults.class);
List<ReportResult> results = reports.getGetReportResult();
//bla..blaa.bblaa..
如需更多详细信息,请查看我的完整代码:
我的活动代码、
我的 ReportResult 类代码、
我的 ReportResults 类代码