相关的代码段:
BasicHttpParams params = new BasicHttpParams();
Object dateFormats = "E, dd-MMM-yyyy HH:mm:ss zzz";
List<Object> dateFormatList = new ArrayList<Object>();
dateFormatList.add(dateFormats);
params.setParameter(CookieSpecPNames.DATE_PATTERNS, dateFormatList);
HttpClient client = new DefaultHttpClient(params);
HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/sample_user.json");
try {
HttpResponse response = client.execute(httpGet);
我已经尝试使用String,一个数组,使用字符串数组上的Arrays.asList来转换List,这是我能想到的一切。但每次我得到类似下面的ClassCastException。我已经浏览了源代码,并且在发生异常的行中,BestMatchSpecFactory使用getParameter()来检索CookieSpecPNames.DATE_PATTERNS(应该是我的List&lt;&gt;)中的参数集,并将其保存到Collection中。 / p>
以下是第49行org.apache.http.impl.cookie中BestMatchSpecFactory.java的代码:
Collection<?> param = (Collection<?>) params.getParameter(CookieSpecPNames.DATE_PATTERNS);
ArrayList实现了实现Collection的List,那么在这里投射的问题究竟是什么?
02-28 10:09:29.117: W/System.err(1222): java.lang.ClassCastException: java.util.ArrayList
02-28 10:09:29.137: W/System.err(1222): at org.apache.http.impl.cookie.BestMatchSpecFactory.newInstance(BestMatchSpecFactory.java:49)
02-28 10:09:29.137: W/System.err(1222): at org.apache.http.cookie.CookieSpecRegistry.getCookieSpec(CookieSpecRegistry.java:113)
02-28 10:09:29.137: W/System.err(1222): at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:147)
02-28 10:09:29.147: W/System.err(1222): at org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290)
02-28 10:09:29.147: W/System.err(1222): at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160)
02-28 10:09:29.167: W/System.err(1222): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
02-28 10:09:29.167: W/System.err(1222): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-28 10:09:29.181: W/System.err(1222): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-28 10:09:29.181: W/System.err(1222): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-28 10:09:29.181: W/System.err(1222): at com.duderlabs.candr.MainActivity.readTwitterFeed(MainActivity.java:80)
02-28 10:09:29.181: W/System.err(1222): at com.duderlabs.candr.MainActivity.onCreate(MainActivity.java:39)
02-28 10:09:29.189: W/System.err(1222): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 10:09:29.217: W/System.err(1222): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-28 10:09:29.217: W/System.err(1222): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-28 10:09:29.217: W/System.err(1222): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-28 10:09:29.217: W/System.err(1222): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-28 10:09:29.238: W/System.err(1222): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 10:09:29.238: W/System.err(1222): at android.os.Looper.loop(Looper.java:123)
02-28 10:09:29.258: W/System.err(1222): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-28 10:09:29.258: W/System.err(1222): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 10:09:29.258: W/System.err(1222): at java.lang.reflect.Method.invoke(Method.java:507)
02-28 10:09:29.258: W/System.err(1222): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-28 10:09:29.258: W/System.err(1222): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-28 10:09:29.258: W/System.err(1222): at dalvik.system.NativeStart.main(Native Method)
更新
我已根据以下答案更新了代码:
BasicHttpParams params = new BasicHttpParams();
String dateFormat = "E, dd-MMM-yyyy HH:mm:ss zzz";
Collection<String> dateFormatList = new ArrayList<String>();
dateFormatList.add(dateFormat);
params.setParameter(CookieSpecPNames.DATE_PATTERNS, dateFormatList);
HttpClient client = new DefaultHttpClient(params);
HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/sample_user.json");
try {
HttpResponse response = client.execute(httpGet);
但仍然得到同样的例外。
答案 0 :(得分:1)
根据Spec,您应该注册CookieSpecPNames.DATE_PATTERNS
(尽管您的代码是正确的):
BasicHttpParams params = new BasicHttpParams();
String dateFormats = "E, dd-MMM-yyyy HH:mm:ss zzz";
java.util.Collection<String> dateFormatList = new ArrayList<String>();
dateFormatList.add(dateFormats);
params.setParameter(CookieSpecPNames.DATE_PATTERNS, dateFormatList);
对于调试,我也会尝试查看是否
client.getParams().setParameter(CookieSpecPNames.DATE_PATTERNS, dateFormatList);
导致ClassCastException
。如果没有,那么您的params
会出现问题。
答案 1 :(得分:0)
client.getParams().setParameter(CookieSpecPNames.DATE_PATTERNS, new String[] { ... });