In my application, I have to get the data from JSON service based on TYPE and SUB TYPE selected by user and show the data to user.
I know how to make an http request, how to parse the response, but the URL is creating an issue.
The URL is like this:
http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={"type":"TYP","sub_type":"SUB"}
I tried escaping the "
with StringEscapeUtils
. But that didn't help.
Sample code:
String baseUrl = "http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={";
String typeInfo = "\"type\":\""+selectedType+"\",\"sub_type\":\""+selectedSubType+"\"}";
typeInfo = StringEscapeUtils.escapeJava(typeInfo);
String finalUrl = baseUrl + typeInfo;
//Call asynctask using finalUrl
....
....
I am getting this exception when I make an http request:
java.lang.IllegalArgumentException: Illegal character in query at index 81: http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={\"type\":\"TYP\",\"sub_type\":\"SUB\"}
Here is what I tried for resolving this:
First:
I tried unescaping the typeInfo
just before I make a call in my AsyncTask like this:
try {
String finalUrl = baseUrl.concat(typeInfo);*/
String finalUrl = StringEscapeUtils.unescapeJava(url[0]);
object = RestJsonClient.connect(finalUrl);
} catch (Exception e) {
e.printStackTrace();
}
This doesn't help. It throws exception as:
java.lang.IllegalArgumentException: Illegal character in query at index 81: http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={"type":"TYP","sub_type":"SUB"}
Second:
Referring this question I tried using UTF-8 encoding according to answer on that post.
Code snippet:
String baseUrl = url[0];
String typeInfo = URLEncoder.encode(url[1], "UTF-8");
String finalUrl = baseUrl.concat(typeInfo);
//String finalUrl = StringEscapeUtils.unescapeJava(url[0]);
object = RestJsonClient.connect(finalUrl);
Here, I passed the URL to asynctask in two Strings instead of one finalUrl string.
This also throws exception such as:
java.lang.IllegalArgumentException: Illegal character in query at index 81: http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={%5C%22type%5C%22%3A%5C%22MANUFACTURER%5C%22%2C%5C%22sub_type%5C%22%3A%5C%22GOLD%5C%22%7D
I am clueless regarding what to do now. It would be great if someone can help.
P.S: The same url shows the result in browser. The url is correct and data is there on server.
EDIT
According to @Tejas's comment, I tried passing the data in a POST request's body like this:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("type", "TYP"));
params.add(new BasicNameValuePair("sub_type", "SUB"));
//request.setEntity(new UrlEncodedFormEntity(params));
object = RestJsonClient.post( baseUrl, params);
Code of RestJsonClient.post
method is working fine in my other calls. So I am not posting all of the code here. The snippet from that method after I get the response is:
HttpEntity httpentity = response.getEntity();
if (httpentity != null) {
InputStream instream = httpentity.getContent();
String result = convertStreamToString(instream);
System.out.println("result: " + result);
json = new JSONObject(result);
instream.close();
}
Now there is no error. I get response from the service with status code OK (200). But no data in result.
08-10 10:33:42.141 7003-9831/jbc.gss.com.jewelnet I/System.out: response in restjson claas- org.apache.http.message.BasicHttpResponse@27ee4f83
08-10 10:33:42.141 7003-9831/pkg.name V/response code: 200
08-10 10:33:42.142 7003-9831/pkg.name I/System.out: result: {"response":"failure","message":" Not data available"}
08-10 10:33:42.150 7003-7003/pkg.name W/System.err: org.json.JSONException: No value for data
Its not possible for me to request the change in service if its required. Can someone help me with passing parameters properly in GET request?