繁体   English   中英

JSONParser错误,找不到类。 Android的

[英]JSONParser error, class not found. Android

我想在我的Android应用程序中从Twitter获取提要。 为了做到这一点,我使用了一些示例代码,现在我无法启动应用程序。 这是代码:

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;

    public class Twitter extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.twitter_layout);

            ArrayList<Tweet> tweets = getTweets("android", 1);

            ListView listView = (ListView) findViewById(R.id.ListViewId);
            listView.setAdapter(new UserItemAdapter(this, R.layout.listitem, tweets));
        }

        public class UserItemAdapter extends ArrayAdapter<Tweet> {
            private ArrayList<Tweet> tweets;

            public UserItemAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) {
                super(context, textViewResourceId, tweets);
                this.tweets = tweets;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.listitem, null);
                }

                Tweet tweet = tweets.get(position);
                if (tweet != null) {
                    TextView username = (TextView) v.findViewById(R.id.username);
                    TextView message = (TextView) v.findViewById(R.id.message);
                    ImageView image = (ImageView) v.findViewById(R.id.avatar);

                    if (username != null) {
                        username.setText(tweet.username);
                    }

                    if(message != null) {
                        message.setText(tweet.message);
                    }

                    if(image != null) {
                        image.setImageBitmap(getBitmap(tweet.image_url));
                    }
                }
                return v;
            }
        }

        public Bitmap getBitmap(String bitmapUrl) {
            try {
                URL url = new URL(bitmapUrl);
                return BitmapFactory.decodeStream(url.openConnection() .getInputStream()); 
            }
            catch(Exception ex) {return null;}
        }

        public ArrayList<Tweet> getTweets(String searchTerm, int page) {
            String searchUrl = "http://search.twitter.com/search.json?q=@" 
                                + searchTerm + "&rpp=100&page=" + page;

            ArrayList<Tweet> tweets = new ArrayList<Tweet>();

            HttpClient client = new  DefaultHttpClient();
            HttpGet get = new HttpGet(searchUrl);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            String responseBody = null;
            try{
                responseBody = client.execute(get, responseHandler);
            }catch(Exception ex) {
                ex.printStackTrace();
            }

            JSONObject jsonObject = null;

            /*  */
            JSONParser pars=new JSONParser();

            /*  */

            try {
                Object obj = pars.parse(responseBody);
                jsonObject=(JSONObject)obj;

            }catch(Exception ex){
                Log.v("TEST","Exception: " + ex.getMessage());
            }

            JSONArray arr = null;

            try {
                Object j = jsonObject.get("results");
                arr = (JSONArray)j;
            }catch(Exception ex){
                Log.v("TEST","Exception: " + ex.getMessage());
            }

            for(Object t : arr) {
                Tweet tweet = new Tweet(
                        ((JSONObject)t).get("from_user").toString(),
                        ((JSONObject)t).get("text").toString(),
                        ((JSONObject)t).get("profile_image_url").toString()
                        );
                tweets.add(tweet);
            }

            return tweets;
        }

        public class Tweet {
            public String username;
            public String message;
            public String image_url;

            public Tweet(String username, String message, String url) {
                this.username = username;
                this.message = message;
                this.image_url = url;
            }
        }
    }

这是我的LogCat:

05-23 11:34:16.443: E/AndroidRuntime(25616): FATAL EXCEPTION: main
05-23 11:34:16.443: E/AndroidRuntime(25616): java.lang.NoClassDefFoundError: org.json.simple.parser.JSONParser
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:673)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.widget.TabHost.setCurrentTab(TabHost.java:345)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:138)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.view.View.performClick(View.java:2408)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.view.View$PerformClick.run(View.java:8816)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.os.Handler.handleCallback(Handler.java:587)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.os.Looper.loop(Looper.java:123)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at android.app.ActivityThread.main(ActivityThread.java:4627)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at java.lang.reflect.Method.invokeNative(Native Method)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at java.lang.reflect.Method.invoke(Method.java:521)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
05-23 11:34:16.443: E/AndroidRuntime(25616):    at dalvik.system.NativeStart.main(Native Method)

我正在使用http://code.google.com/p/json-simple/

您需要在项目的LibLibs目录中使用json-simple jar。 您可能目前已将其设置为可以将jar用于构建,但不包含在构建的应用程序中。

你应该导入,

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

并使用像

JSONArray aryJSONStrings = new JSONArray("your json string");
for (int i=0; i<aryJSONStrings.length(); i++) {
       NewsItemId = aryJSONStrings.getJSONObject(i).getString("your_attribute_name");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM