I am trying to use the json-lib-2.4-jdk15.json. I don’t have any error in my code but when I run my program the catlog :
Caused by: java.lang.NoClassDefFoundError: net.sf.json.JSONSerializer
Whereas I have “import net.sf.json.JSONSerializer;” and eclipse don’t show any error at this line.
I added the .jar like this : Configure Build path -> add external jar .. (so now the jar appear in Referenced Libraries)
My class :
package com.example.androidgridweb;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import android.os.AsyncTask;
public class eSearchElastic{
public void ESE (final ImageAdapter imgAdapter)throws ClientProtocolException, IOException {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
String server = "servename";
String index = "images";
String type = "images_schema_1";
StringEntity query = null;
try {
query = new StringEntity("{\"sort\" : [ {\"confidence_level\" : {\"order\" : \"desc\"} }],\"from\" : 0, \"size\" : 10,\"query\" : {\"text_phrase\" : { \"keyword\" : \"finding nemo\"}},\"filter\" : {\"numeric_range\" : {\"confidence_level\" : { \"from\" : 10, \"to\" : 100, \"include_lower\" : true, \"include_upper\" : true}}}}'");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i=0;
HttpClient httpclient = new DefaultHttpClient();
StringBuilder urlStr = new StringBuilder("http://");
urlStr.append(server)
.append("/" + index)
.append("/" + type + "/_search");
System.out.println(urlStr.toString());
while (i < 5) {
HttpPost httpost = new HttpPost(urlStr.toString());
httpost.setEntity((HttpEntity)query);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
HttpResponse response = null;
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String answer = null;
try {
answer = org.apache.http.util.EntityUtils.toString(response.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(answer);
i++;
JSONObject answerJson = (JSONObject) JSONSerializer.toJSON(answer);
String urlImage;
try {
urlImage = answerJson.getString("hits");
JSONObject answerJson1 = (JSONObject) JSONSerializer.toJSON(urlImage);
String urlImage1 = answerJson1.getString("hits").replace("[", "").replace("]","");
JSONObject answerJson2 = (JSONObject) JSONSerializer.toJSON(urlImage1);
String urlImage2 = answerJson2.getString("_source");
JSONObject answerJson3 = (JSONObject) JSONSerializer.toJSON(urlImage2);
String urlImage3 = answerJson3.getString("url");
System.out.println(urlImage3);
imgAdapter.addmThumbIds(urlImage3);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
};
task.execute();
}
}
Catlog:
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.NoClassDefFoundError: net.sf.json.JSONSerializer
at com.example.androidgridweb.eSearchElastic$1.doInBackground(eSearchElastic.java:82)
at com.example.androidgridweb.eSearchElastic$1.doInBackground(eSearchElastic.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
Adding the jar to the build path doesn’t mean the jar will be included in the application apk. This is why it works at compile time (eclipse), and it doesn’t at run time.
Create a “libs” folder at the top level of you android app project, and copy the jar inside that folder. The ADT plugin will automatically add any jar’s in that folder to your eclipse buildpath and to the built apk file.