Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send values by GET method

Tags:

android

I have been trying to send data to server through GET method but I am unable to find a way to do it. I have tried few codes in asynchronous task but nothing. The web services are made in cakePhp and the format is like this:

Base_URI/users/add.json?json={“email”: [email protected], “password”: “xxxxxxxxx”, “first_name”: “Xyz”, “last_name”: “Xyz”}

Android experts are requested to figure a way out of this problem. Thanks

Here is the code:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("email", UserDummy.email));
            nameValuePairs.add(new BasicNameValuePair("password", UserDummy.password));
            nameValuePairs.add(new BasicNameValuePair("first_name", UserDummy.fname));
            nameValuePairs.add(new BasicNameValuePair("last_name", UserDummy.lname));
            // Making HTTP request
    try {

        // check for request method
        if (method == "POST") {
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } else if (method == "GET") {
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, HTTP.UTF_8);
            url += "?json={" + paramString+"}";                                                                                                                                                     ;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        Log.v("XXX", e.getMessage());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Log.v("XXX", e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        Log.v("XXX", e.getMessage());
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

HttpGet is not accepting this format of url and is giving the error but when I try it on browser it works fine. Error is following:

Illegal character in query at index 56
like image 729
Shahzeb Avatar asked Jan 18 '14 19:01

Shahzeb


People also ask

Can I send data using GET method?

Can I send data using the HTTP GET method? No, HTTP GET requests cannot have a message body. But you still can send data to the server using the URL parameters. In this case, you are limited to the maximum size of the URL, which is about 2000 characters (depends on the browser).

Can we pass parameter in GET method?

Parameters can be passed in GET Request, if you are not sure how to do a GET Request using Postman, please take a look at the previous article How to make a GET Request. Since now you know how to make a GET request, we will move ahead with sending parameters in a GET request.

How do I send an argument in GET request?

To do http get request with parameters in Angular, we can make use of params options argument in HttpClient. get() method. Using the params property we can pass parameters to the HTTP get request. Either we can pass HttpParams or an object which contains key value pairs of parameters.


1 Answers

Finally after debugging and trying different solutions the whole day, I solved the problem :)

I needed to encode the parameters part and not the whole URL like this:

String url = "Base_URI/users/add.json?json=";
    url =url +  URLEncoder.encode("{\"email\":\""+email+"\",\"password\":\""+password+"\"}", "UTF-8");

Thanks everyone for your support !

like image 90
Shahzeb Avatar answered Oct 21 '22 01:10

Shahzeb