0

我想使用 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 类代码

4

2 回答 2

8

确保将 gson.jar 放在libs目录中。从 ADT17 开始,所有外部库 jar 都必须放在那里。

好消息是您只需将它们放在那里,工具就会负责将它们添加到项目中。

哦,该目录需要与srcandassets等处于同一级别。

于 2012-06-29T02:11:36.773 回答
0

在项目的根目录中创建“libs”文件夹,其中保存“src”和“assets”文件夹,将 jar 文件放入其中,仅此而已。现在你可以使用它们了。确保撤消您尝试进行的所有更改。:)

于 2015-09-07T09:16:04.133 回答