java.lang.illegalargumentexception illegal charact

2020-04-09 20:17发布

i want to request into web service
my parameter like this

urlString = http://ip/autodownload/andro.php?key=apps.apk|2|bla.bla.bla

public void getRequest(String Url) {
    Toast.makeText(this, "Tambah Data " + Url + " ", Toast.LENGTH_SHORT).show();
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlString);
    try {
        System.out.println("tes");
        HttpResponse response = client.execute(request);
        Toast.makeText(this,request(response),Toast.LENGTH_SHORT).show();
        String res = EntityUtils.toString(response.getEntity());
        System.out.println(res);
        Update(res);
    } catch (Exception ex) {
        Toast.makeText(this, "Gagal Konek Server !", Toast.LENGTH_SHORT).show();
    }       
}

if i run in browser it's ok, but when i run in android it's error like this

10-25 10:24:49.862: ERROR/AndroidRuntime(14602): FATAL EXCEPTION: main
10-25 10:24:49.862: ERROR/AndroidRuntime(14602): java.lang.IllegalArgumentException: Illegal character in query at index 67: http://10.234.152.120/autodownload/andro.php?key=DeliverReceipt.apk|2|com.sat.deliver
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at java.net.URI.create(URI.java:970)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at com.sat.deliver.MenuUtama.getRequest(MenuUtama.java:140)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at com.sat.deliver.MenuUtama.requestParam(MenuUtama.java:118)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at com.sat.deliver.MenuUtama.onClick(MenuUtama.java:355)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at android.view.View.performClick(View.java:2408)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at android.view.View$PerformClick.run(View.java:8816)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at android.os.Handler.handleCallback(Handler.java:587)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at android.os.Looper.loop(Looper.java:123)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at java.lang.reflect.Method.invoke(Method.java:521)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-25 10:24:49.862: ERROR/AndroidRuntime(14602):     at dalvik.system.NativeStart.main(Native Method)

i have try with Urlencoding, and replacement character, but it's not working
what should i do?thank you
*SOLVED
i make replace character in urlString like :

urlString+="?key="+appName.trim().replace(".", "%2E")+"|2|".trim().replace("|", "%7C")+packageName.trim().replace(".", "%2E");

and it's work fine :)

标签: android
4条回答
我欲成王,谁敢阻挡
2楼-- · 2020-04-09 20:21

This worked for me. Suppose you have illegal character inside your IP address. Then convert it into Base64 code and send to your server.

For example:

String ip_address = StaticClass.getIPAdress();
        try {

            // Sending side
            byte[] data = ip_address.getBytes("UTF-8");
            ip_address = Base64.encodeToString(data, Base64.DEFAULT);

        } catch (Exception e) {
            e.printStackTrace();
            ip_address = "";
        }

Write encode ip_address or any field you want inside your url. Decode the variables at your server. But i'm not sure if you are using : com.loopj.android.http.AsyncHttpClient.get

vkj

查看更多
淡お忘
3楼-- · 2020-04-09 20:25

Try encoding Your URL

String link="http://example.php?string1="+URLEncoder.encode(string1)+"&string2="+URLEncoder
.encode(string2)+"&string3="+URLEncoder.encode(string3)+"&string4="+URLEncoder.encode(string4)+"";
查看更多
爷的心禁止访问
4楼-- · 2020-04-09 20:32

In my case it was the variable that I was appending that caused the problem. It had an extra space (spaces). I trimmed it and the error went away. "http://somewebsite.com/somefile?q="+someVariable.trim()

查看更多
▲ chillily
5楼-- · 2020-04-09 20:45

Check this

String myurltoencode = "http://ip/autodownload/andro.php?key=apps.apk|2|bla.bla.bla";
URLEncoder.encode(myurltoencode,"UTF-8"); 
查看更多
登录 后发表回答