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..

詳細なレビューについては、ここに私の完全なコードがあります:
私のアクティビティコード
MyReportResultクラスコード
MyReportResultsクラスコード

4

2 に答える 2

8

gson.jar をlibsディレクトリに置いていることを確認してください。ADT17 以降、すべての外部ライブラリ jar はそこに移動する必要があります。

良いニュースは、それらをそこにドロップするだけで、ツールがそれらをプロジェクトに追加することを処理します.

srcああ、そのディレクトリはやassetsなどと同じレベルにある必要があります。

于 2012-06-29T02:11:36.773 に答える
0

プロジェクトのルートに「src」と「assets」フォルダーが保存されている「libs」フォルダーを作成し、その中に jar ファイルを配置するだけです。これで、それらを使用できます。実行しようとしたすべての変更を元に戻してください。:)

于 2015-09-07T09:16:04.133 に答える