blankon91
blankon91

Reputation: 515

Why my application can not find class 'com.google.gson.Gson' on the OnCreate method?

I want to use Gson to parse my JSON, I've added Gson library into my project. It's all fine when I compile it, but when I run it, I get error message log says

Could not find class 'com.google.gson.Gson', referenced from method report.weeklyflash.WeeklyFlashIdActivity.onCreate

here is the code that use 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..

for more detail review, here is my full code:
My Activity code
My ReportResult Class code
My ReportResults Class code

Upvotes: 0

Views: 7490

Answers (2)

Karan Datwani
Karan Datwani

Reputation: 775

Make "libs" folder in root of your project where "src"&"assets" folders are kept, put jar files in it, thats all. Now you can use them. Make sure you undo all changes you tried to make it. :)

Upvotes: 0

Barak
Barak

Reputation: 16393

Make sure you put the gson.jar in the libs directory. As of ADT17 all external library jars must go there.

The good news is you just drop them in there and the tools take care of adding them to the project.

Oh, that directory needs to be on the same level as src and assets, etc.

Upvotes: 8

Related Questions