【问题标题】:How should I put json in GET request?我应该如何将 json 放入 GET 请求中?
【发布时间】:2014-05-05 15:17:49
【问题描述】:

我想通过 spring 为 android 实现 GET 请求。我不知道应该如何将 json 放在 url 中。网址是:

context.getString(R.string.url) + "services/promotions?limit=10&where={expiredAt:{$gte: \""+getCurrentDate()+"\"}}"; 我想设置但请求失败并在 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}\^

有什么问题?

更新:我使用该网址的课程是:

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());
} 
}

【问题讨论】:

    标签: android regex json spring-android


    【解决方案1】:

    您需要将 json 编码为 URL,然后与服务 URL 连接。此外,您还需要使用 " 引用密钥。

    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

    【讨论】:

    • 对不起,我还有一个问题,因为我使用的是spring,url编码了两次然后请求失败。
    • 我不知道 spring dude..,但我知道两次 URL 编码不起作用。您只需一次对 json 进行编码。
    猜你喜欢
    • 1970-01-01
    • 2016-01-13
    • 2017-11-03
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多