1

我正在尝试使用 json-lib-2.4-jdk15.json。我的代码没有任何错误,但是当我运行程序时, catlog :

Caused by: java.lang.NoClassDefFoundError: net.sf.json.JSONSerializer

而我有“import net.sf.json.JSONSerializer;” 并且 eclipse 在这一行没有显示任何错误。我像这样添加了.jar:配置构建路径->添加外部jar ..(所以现在jar出现在引用库中)

我的课 :

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();

}

}

目录:

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)
4

1 回答 1

4

将 jar 添加到构建路径并不意味着该 jar 将包含在应用程序 apk 中。这就是为什么它在编译时(eclipse)起作用,而在运行时不起作用。

在您的 android 应用程序项目的顶层创建一个“libs”文件夹,并将 jar 复制到该文件夹​​中。ADT 插件会自动将该文件夹中的任何 jar 添加到您的 eclipse 构建路径和构建的 apk 文件中。

于 2012-11-27T18:40:49.600 回答