Json的非法参数异常

时间:2011-10-15 00:10:22

标签: android json

运行我的代码时出现此错误,我没有从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)

基本上我正在尝试使用json,而我遇到的问题是这行

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

我已经对字符串进行了编码,但我仍然得到了异常

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();
            } 

任何解决方案?

3 个答案:

答案 0 :(得分:0)

您可能需要对参数字符串进行URL编码。尝试使用URLEncoder

顺便说一下,角色68是=

此外,是否存在超出此警告的问题(例如,您没有获得所需的结果)?注意 - 这不是错误,而是警告。

答案 1 :(得分:0)

问题是花括号。我希望URLEncoder能够处理它,但你也可以手动替换它们。 '{'应为%7B,'}'应为“%7D”。

答案 2 :(得分:0)

这可能有助于你

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