我试图通过loopj的AsyncHttpClient从facebook图形API获得响应,但我遇到了编码问题。
https://graph.facebook.com/v2.1/Google?fields=posts.limit(15){message,object_id,type,updated_time},name,picture&access_token=########|########
当我将此网址传递给get()时,我得到IllegalArgumentException
:
Illegal character in query at index 82: https://graph.facebook.com/v2.1/Google?fields=posts.limit(15){message,object_id,type,updated_time},name,picture&access_token=########|########
java.lang.IllegalArgumentException: Illegal character in query at index 82: ttps://graph.facebook.com/v2.1/Google?fields=posts.limit(15){message,object_id,type,updated_time},name,picture&access_token=########|########
at java.net.URI.create(URI.java:727)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
at com.loopj.android.http.AsyncHttpClient.get(AsyncHttpClient.java:612)
at com.loopj.android.http.AsyncHttpClient.get(AsyncHttpClient.java:599)
at com.aufmschoolbliz.app.activities.TestFacebookFragment.onCreateView(TestFacebookFragment.java:60)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我尝试通过setURLEncodingEnabled()
将URL编码设置为true和false,但它提供了相同的异常。还尝试过URLEncoder类,但没有运气。
答案 0 :(得分:0)
我通过将URL的部分传递给URI的构造函数来修复此问题:
URL url = new URL("the graph url");
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
return url.toString();