3

可能重复:
如何阻止 HTTP 转义引号?

我正在创建一个 JSONObject 并将 JSON 字符串发送到 POST 请求正文中的服务器。

public String toJson() {
    JSONObject filter = new JSONObject();
    try {
        filter.put("gender", gender.getCode());
        filter.put("feature_id", productCategory);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject filterObject = new JSONObject();
    try {
        filterObject.put("filter", filter);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return filterObject.toString();
}

所以我正在创建一个请求:

private IJsonExecutorInterface requestExecutorForRelativePathAndParams(String path, WebParams params) throws UnsupportedEncodingException {
    HttpPost postRequest = new HttpPost(rootUrl + path);

    if(params != null) {
        postRequest.setHeader("content-type", params.getContentType());
        postRequest.setEntity(params.getFormEntity());
    }

    // Blah blah

    return executor;
}

public IJsonExecutorInterface getProducts(ProductFilter filter, int offset, int limit) throws UnsupportedEncodingException {
    WebParams webParams = new WebParams();
    webParams.addPair("filter", filter.toJson());
    webParams.addPair("offset", String.format("%d", offset));
    webParams.addPair("limit", String.format("%d", limit));
    return requestExecutorForRelativePathAndParams("products", webParams);
}

// WebParams class


public class WebParams {
    private ArrayList<NameValuePair> params;
    private String contentType = "application/x-www-form-urlencoded";

    public WebParams() {
        params = new ArrayList<NameValuePair>();
    }

    public void addPair(String name, String value) {
        params.add(new BasicNameValuePair(name, value));
    }

    public String getContentType() {
        return contentType;
    }

    public HttpEntity getFormEntity() throws UnsupportedEncodingException {
        return new UrlEncodedFormEntity(params);
    }
}

我在调试器中看到它:没关系。

但是在我的服务器上,我得到了这样的东西:

Array
(
    [filter] => {\"gender\":\"w\",\"feature_id\":\"41_7459\"}
    [offset] => 0
    [limit] => 18
)

引号已转义。

我不想替换服务器上的东西。replace("\\\"", "\"")在 Java 中不会影响字符串。

4

2 回答 2

1

看起来您使用的是 UrlEncodedFormEntity,根据文档,它是“由 url 编码对列表组成的实体”([http://developer.android.com/reference/org/apache/http/client/entity /UrlEncodedFormEntity.html])。我从来没有使用过这个,但它听起来不像你想要的,因为你是在帖子正文中发送数据,而不是通过 URL 参数。

我之前使用过这个StringEntity类来通过 post 发送 json 数据,虽然它只编码一个字符串,而不是名称/值对,所以你必须做更多的工作来把字符串放在你想要处理的格式在您的服务器上:

public class WebParams {
    private ArrayList<NameValuePair> params;
    private String contentType = "application/x-www-form-urlencoded";

    public WebParams() {
        params = new ArrayList<NameValuePair>();
    }

    public void addPair(String name, String value) {
        params.add(new BasicNameValuePair(name, value));
    }

    public String getContentType() {
        return contentType;
    }

    public HttpEntity getFormEntity() throws UnsupportedEncodingException {
        //TODO: Build a string in what ever format you want. 
        //      This will include the gender & feature_id fields as well as the json
        StringBuilder b = new StringBuilder();
        for(NameValuePair nvp : params) {
           builder.append(nvp.getName()).append('=').append(nvp.getValue()).append(',');
        }

        //Now that we have a string to send to the server, get your entity!
        StringEntity entity = new StringEntity(b.toString());
        entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        return entity;
    }
}
于 2012-06-25T19:11:33.257 回答
0

使用单引号代替双引号有问题吗?因为我认为它会解决你的问题。

于 2012-06-25T18:38:33.707 回答