我想在我的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: main05-23 11:34:16.443: E/AndroIDRuntime(25616): java.lang.NoClassDefFoundError: org.Json.simple.parser.JsONParser05-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.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/.
解决方法:
您需要在项目的lib或libs目录中使用Json-simple jar.您可能目前已将其设置为可以将jar用于构建,但不包含在构建的应用程序中.
总结以上是内存溢出为你收集整理的JSONParser错误,找不到类. Android的全部内容,希望文章能够帮你解决JSONParser错误,找不到类. Android的所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)