简体   繁体   中英

Illegal argument exception with Json

I get this error when running my code, and I don't receive anything back from json

10-15 00:29:22.396: WARN/System.err(562): java.lang.IllegalArgumentException: Illegal character in query at index 68: http://www.hotels-in-london-hotels.com/mytrolly/service.php?request={"mode":"category"}
10-15 00:29:22.425: WARN/System.err(562):     at java.net.URI.create(URI.java:970)
10-15 00:29:22.425: WARN/System.err(562):     at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
10-15 00:29:22.436: WARN/System.err(562):     at com.sampleapp.MainActivity$iTab.readTwitterFeed(MainActivity.java:128)
10-15 00:29:22.436: WARN/System.err(562):     at com.sampleapp.MainActivity$iTab.<init>(MainActivity.java:65)
10-15 00:29:22.436: WARN/System.err(562):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-15 00:29:22.446: WARN/System.err(562):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
10-15 00:29:22.446: WARN/System.err(562):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
10-15 00:29:22.456: WARN/System.err(562):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
10-15 00:29:22.456: WARN/System.err(562):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
10-15 00:29:22.466: WARN/System.err(562):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-15 00:29:22.466: WARN/System.err(562):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-15 00:29:22.466: WARN/System.err(562):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-15 00:29:22.476: WARN/System.err(562):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
10-15 00:29:22.476: WARN/System.err(562):     at android.app.Activity.setContentView(Activity.java:1647)
10-15 00:29:22.476: WARN/System.err(562):     at com.sampleapp.MainActivity.onCreate(MainActivity.java:362)
10-15 00:29:22.486: WARN/System.err(562):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-15 00:29:22.486: WARN/System.err(562):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-15 00:29:22.486: WARN/System.err(562):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-15 00:29:22.496: WARN/System.err(562):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-15 00:29:22.496: WARN/System.err(562):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-15 00:29:22.496: WARN/System.err(562):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 00:29:22.506: WARN/System.err(562):     at android.os.Looper.loop(Looper.java:123)
10-15 00:29:22.506: WARN/System.err(562):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-15 00:29:22.506: WARN/System.err(562):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 00:29:22.506: WARN/System.err(562):     at java.lang.reflect.Method.invoke(Method.java:521)
10-15 00:29:22.506: WARN/System.err(562):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-15 00:29:22.506: WARN/System.err(562):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-15 00:29:22.506: WARN/System.err(562):     at dalvik.system.NativeStart.main(Native Method)

Basically I'm trying to use json, and the problem I'm having is with this line

HttpGet httpGet = new HttpGet(
                    "http://www.hotels-in-london-hotels.com/mytrolly/service.php?request={\"mode\":\"category\"}");

I've encoded the string, and I still get the exception

try {
                url = "http://www.hotels-in-london-hotels.com/mytrolly/service.php?request={\"mode\":\"category\"}";
                String encodedurl = URLEncoder.encode(url,"UTF-8");
                Log.d("TEST", encodedurl);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } 

Any solutions?

This may helps you

String link="http://example.php?string1="+URLEncoder.encode(string1)+"&string2="+URLEncoder
.encode(string2)+"&string3="+URLEncoder.encode(string3)+"&string4="+URLEncoder.encode(string4)+"";

You probably need to URL encode your parameter string. Try using URLEncoder

Character 68 is = , by the way.

Also, is there a problem beyond this warning (like you don't get desired result)? Note - it's not an error but warning.

The problem is the curly braces. I'd expect URLEncoder to take care of it, but you can also just manually replace them. '{' should be %7B and '}' should be "%7D".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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