2012-06-25 42 views
3

可能重複:
How can I stop HTTP from escaping quotes?的Java將在POST請求中的JSON字符串轉義雙引號

我創建一個JSONObject並在POST請求體發送JSON字符串到服務器。

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 
) 

引號轉義。

我不想替換服務器上的某些東西。 Java中的replace("\\\"", "\"")不影響字符串。

+0

答案是http://stackoverflow.com/questions/6715650/how-can-i-stop-http-from-escaping-quotes。這不是Java問題。這是一個PHP問題。 – efpies

+0

我現在使用Retrofit lib時遇到同樣的問題,您是否找到了解決方案或者您是否必須在服務器端處理它? –

回答

0

使用簡單引號而不是雙引號有問題嗎?因爲我認爲它會解決你的問題。

+0

我必須使用雙引號。 JSON字段名稱放在引號中。 – efpies

+1

是的,但(至少在JavaScript中解析它時),簡單引號與雙引號一樣有效。試試看。 – YuriAlbuquerque

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; 
    } 
} 
+0

謝謝。但不幸的是,我不得不在POST請求中發送其他參數以及JSON,並且它們都應該使用'application/x-www-form-urlencoded'內容類型。所以我必須替換服務器上的轉義引號... – efpies

相關問題