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”: xxx@x.com, “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