我应该如何将JSON放入GET请求中? [英] How should I put json in GET request?
问题描述
我想在Spring之前为Android实现GET请求.我不知道该如何将json放在url中.网址是:
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()+"\"}}";
,但请求失败,并在logcat上收到此错误:
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}\^
出什么问题了?
更新:我使用该网址的课程是:
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());
}
}
推荐答案
您需要将json编码为URL,然后与服务URL串联.另外,您还需要用"
引用密钥.
you need to encode json to URL and then concatenate with service URL. Also you would need to quote key with "
.
String json_to_url=URLEncoder.encode("{\"expiredAt\":{\"$gte\":\"" + getCurrentDate() + "\"}}", "utf-8");
String serviceURL=context.getString(R.string.url) + "services/promotions?limit=10&where="+ json_to_url;
更好的方法是使用POST
代替GET
Better approach would be using POST
to submit data instead GET
这篇关于我应该如何将JSON放入GET请求中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!