Search code examples
javaandroidgetconnection-string

Android not considering the complete GET parameters in the URL


What is the basic working scenario..?

If i try below URL from web browser, it works fine, sends a message.

[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 : Do not try it, as i have changed the username and passwords..!!

What is next..?

I want to make same GET request call, but from my android studio java project.

What i have tried..?

As network operations are not allowed from main thread, i am implementing the same in a Runnable Thread. The code is as below :

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 is not considering the Unicode Text as the part of the URL, So it breaks the URL, and opening connection with the partial URL.

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)

In the above log-cat android is considering http://49.50.67.32/smsapi/httpapi.jsp?username=myusername&password=mypassword&from=myfrom&to=919970316209&text= as a underlined URL, then येथे+क्लिक+करा+ as text and again https://www.google.co.in/&coding=2 as the second URL.


Solution

  • The problem is solved, it was because of the URL was not encoded before making the request, solved by :

    URLEncoder.encode(Text, "UTF-8")
    

    I got the reference from Java URL encoding of query string parameters

    The accepted answer of above thread, solved my issue too, Thanks a lot to the user @BalusC