-1

I want to make a HTTP request to the url below.

http://192.168.1.60/api/check/merge?nodeid=2&tickets=2-0011,2-0010&saleId=140708120131003102,140708115753005302&firsttableid=1&layoutid=1&userid=1

I stored the tickets details and sales id details in the string array.

String tickets has {2-0011,2-0010} and String saleid has {140708120131003102, 140708115753005302}

How to form the URL like this by using that string array?

String mergeurl1 = "http://192.168.1.60/api/check/merge?nodeid=2&tickets=";
                try
                {
                    for(int i=0;i<tablenameurl.length;i++)
                    {
                        //System.out.println(ticketidurl[i]+"**"+salesidurl[i]);
                        if(tablenameurl.length==i && tablenameurl != null)
                        {
                            mergeurl1=mergeurl1+ticketidurl[i]+"&";
                        }
                        else
                        {
                            mergeurl1=mergeurl1+ticketidurl[i]+",";
                        }
                    }
                    mergeurl1=mergeurl1+"saleId=";
                    for(int i=0;i<tablenameurl.length;i++)
                    {
                        //System.out.println(ticketidurl[i]+"**"+salesidurl[i]);
                        if(tablenameurl.length==i)
                        {
                            mergeurl1=mergeurl1+salesidurl[i]+"&";
                        }
                        else
                        {
                            mergeurl1=mergeurl1+salesidurl[i]+",";
                        }
                    }
                    mergeurl1=mergeurl1+"&firsttableid=1&layoutid=1&userid=1";
                    System.out.println("URL");
                    System.out.println(mergeurl1);
                }
                catch(Exception e)
                {

                }
user3789180
  • 21
  • 1
  • 6

2 Answers2

0

You have to use a URL Encoder for this purpose. The best way to do that would be to URLEncode and encode parts of the url. Dont encode the complete URL, it wont work then. You only have to encode the parts of the url that need to be passed as values. For example in this case encode 2-0011,2-0010

Hiemanshu Sharma
  • 7,702
  • 1
  • 16
  • 13
0

OK, assuming you have the URL String mergeurl1

URL url = new URL(mergeurl1);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

If you want to obtain the response ...

InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//go ahead to process your information with the inputstream

Finally close the HttpURLConnection by urlConnection.disconnect();

Please note that all the code the above cannot be run in your UI Thread, you may use a AsyncTask to cater that.

Here is the reference for you to learn about that:)

http://developer.android.com/reference/android/os/AsyncTask.html
http://developer.android.com/reference/java/net/HttpURLConnection.html

By the way, as the comment above mentioned, you better use a StringBuffer to append the String

hungr
  • 2,086
  • 1
  • 20
  • 33