Android 不考虑 URL 中的完整 GET 参数
Android not considering the complete GET parameters in the URL
What is the basic working scenario..?
如果我在 URL
下面尝试从 web browser
,它工作正常,发送消息。
[http://49.50.67.32/smsapi/httpapi.jsp?username=myusername&password=mypassword&from=myfrom&to=919970316209&text=येथे+क्लिक+करा+https://www.google.co.in/&coding=2][1]
** P.N : 不要尝试,因为我已经更改了用户名和密码..!!
What is next..?
我想发出相同的 GET 请求调用,但是来自我的 android 工作室 java 项目。
What i have tried..?
由于主线程不允许进行网络操作,因此我在 Runnable Thread
中实现了相同的功能。代码如下:
String coding = "2";
String number = "919970316209";
String Text = "येथे+क्लिक+करा+https://www.google.co.in/";
String dummyURL = "http://49.50.67.32/smsapi/httpapi.jsp?username=myusername&password=mypassword&from=myfrom&to="+number+"&text="+Text+"&coding="+coding;
URL url = new URL(dummyURL);
connection = url.openConnection();
InputStream is = null;
is = connection.getInputStream();
Log.d("SendViaGate : ", "OPENING : "+ url +" \n");
// FROM HERE ONWARD I GET THE RESPONSE FROM THIS REQUEST
What is the problem..?
Android 没有将 Unicode Text
视为 URL 的一部分,因此它打破了 URL,并打开与部分 [=50= 的连接].
LOG_CAT
04-07 18:34:41.253 7491-7584/com.versatilemarketers.apps.instafame D/SendViaGate :: Exception occurred in main thread..
04-07 18:34:41.254 7491-7584/com.versatilemarketers.apps.instafame W/System.err: java.io.FileNotFoundException: http://49.50.67.32/smsapi/httpapi.jsp?username=myusername&password=mypassword&from=myfrom&to=919970316209&text=येथे+क्लिक+करा+https://www.google.co.in/&coding=2
04-07 18:34:41.255 7491-7584/com.versatilemarketers.apps.instafame W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
at com.versatilemarketers.apps.instafame.SendViaGate.run(SendViaGate.java:51)
在上面的log-cat中android正在考虑http://49.50.67.32/smsapi/httpapi.jsp?username=myusername&password=mypassword&from=myfrom&to=919970316209&text=
作为带下划线的 URL,然后是 येथे+क्लिक+करा+ 作为 text
,然后 https://www.google.co.in/&coding=2 作为第二个 URL。
问题已解决,是因为URL在发出请求前没有编码,解决方法:
URLEncoder.encode(Text, "UTF-8")
我从 Java URL encoding of query string parameters
那里得到了参考
上述帖子的接受答案,也解决了我的问题,非常感谢用户@BalusC
What is the basic working scenario..?
如果我在 URL
下面尝试从 web browser
,它工作正常,发送消息。
[http://49.50.67.32/smsapi/httpapi.jsp?username=myusername&password=mypassword&from=myfrom&to=919970316209&text=येथे+क्लिक+करा+https://www.google.co.in/&coding=2][1]
** P.N : 不要尝试,因为我已经更改了用户名和密码..!!
What is next..?
我想发出相同的 GET 请求调用,但是来自我的 android 工作室 java 项目。
What i have tried..?
由于主线程不允许进行网络操作,因此我在 Runnable Thread
中实现了相同的功能。代码如下:
String coding = "2";
String number = "919970316209";
String Text = "येथे+क्लिक+करा+https://www.google.co.in/";
String dummyURL = "http://49.50.67.32/smsapi/httpapi.jsp?username=myusername&password=mypassword&from=myfrom&to="+number+"&text="+Text+"&coding="+coding;
URL url = new URL(dummyURL);
connection = url.openConnection();
InputStream is = null;
is = connection.getInputStream();
Log.d("SendViaGate : ", "OPENING : "+ url +" \n");
// FROM HERE ONWARD I GET THE RESPONSE FROM THIS REQUEST
What is the problem..?
Android 没有将 Unicode Text
视为 URL 的一部分,因此它打破了 URL,并打开与部分 [=50= 的连接].
LOG_CAT
04-07 18:34:41.253 7491-7584/com.versatilemarketers.apps.instafame D/SendViaGate :: Exception occurred in main thread..
04-07 18:34:41.254 7491-7584/com.versatilemarketers.apps.instafame W/System.err: java.io.FileNotFoundException: http://49.50.67.32/smsapi/httpapi.jsp?username=myusername&password=mypassword&from=myfrom&to=919970316209&text=येथे+क्लिक+करा+https://www.google.co.in/&coding=2
04-07 18:34:41.255 7491-7584/com.versatilemarketers.apps.instafame W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
at com.versatilemarketers.apps.instafame.SendViaGate.run(SendViaGate.java:51)
在上面的log-cat中android正在考虑http://49.50.67.32/smsapi/httpapi.jsp?username=myusername&password=mypassword&from=myfrom&to=919970316209&text=
作为带下划线的 URL,然后是 येथे+क्लिक+करा+ 作为 text
,然后 https://www.google.co.in/&coding=2 作为第二个 URL。
问题已解决,是因为URL在发出请求前没有编码,解决方法:
URLEncoder.encode(Text, "UTF-8")
我从 Java URL encoding of query string parameters
那里得到了参考上述帖子的接受答案,也解决了我的问题,非常感谢用户@BalusC