I want to implement GET request by spring for android. I do not know how should put json in the url for that. The url is:
context.getString(R.string.url) + "services/promotions?limit=10&where={expiredAt:{$gte: \""+getCurrentDate()+"\"}}";
that I want to set but the the request fails and got this error on logcat:
Syntax error in regexp pattern near index \Qhttp://baas.papata.ir/services/promotions?limit=1&where=\E({$gte: "2014-05-05T14:48:20Z")\Q}\^
What is the problem?
Update: My class that I use that url is:
public class GetPromotion extends AsyncTask<Void, Void, Promotion[]> {
private String SERVICE_ID = "5345345";
private String API_VERSION = "0.1.0";
private String url;
private String clientId;
private Context context;
private OnGetPromotion onGetPromotion;
public GetPromotion(Context context, String clientId){
url = context.getString(R.string.url) + "services/promotions?limit=10&where={expiredAt:{$gte: \""+getCurrentDate()+"\"}}";
this.clientId = clientId;
this.context = context;
}
@Override
protected Promotion[] doInBackground(Void... voids) {
try {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("firstParam", API_VERSION);
requestHeaders.add("secondParam", SERVICE_ID);
requestHeaders.add("Cache-Control", "no-cache");
requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
RestTemplate restTemplate = new RestTemplate(true);
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
restTemplate.getMessageConverters().add(
new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(
new StringHttpMessageConverter());
ResponseEntity<Promotion[]> response = restTemplate
.exchange(url, HttpMethod.GET, requestEntity,
Promotion[].class);
return response.getBody();
} catch (Exception e)
return null;
}
}
@Override
protected void onPostExecute(Promotion[] result) {
super.onPostExecute(result);
onGetPromotion.getOnGetPromotion(result);
}
private String getCurrentDate() {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(tz);
return df.format(new Date());
}
}
you need to encode json to URL and then concatenate with service URL. Also you would need to quote key with
"
.Better approach would be using
POST
to submit data insteadGET