gpt4 book ai didi

java - Android 如何将 JSON 对象作为 url 参数传递

转载 作者:行者123 更新时间:2023-12-01 06:18:16 26 4
gpt4 key购买 nike

我有一个要求,我必须在 url 中传递一个 json 对象作为参数。这是我的网址

http://test.amazonaws.com/xyx/company/12/user/1/contact/{contactActivityJSON}/key/abccedrf/contacts?op=readsave

现在 contactActivityJSON 具有以下结构

{
"user": {},
"event": {
"id": 1,
},
"contacts": [
{
"id": "-1",
"eventId": 1,
"fields": [
{
"fieldName": "test",
"value": "CallMe"
}
]
}
]
}

现在我在将 JSON 对象编码到 URL 时遇到问题。我正在使用

ObjectMapper mapper = new ObjectMapper();
Writer strWriter = new StringWriter();
mapper.writeValue(strWriter, request.getRequestBody());
String jsonString = strWriter.toString();
String sss = URLEncoder.encode(jsonString);

并将其替换为 URL。但它不起作用。有人可以帮忙吗?

最佳答案

我猜您正在将 JsonObject 发布到服务器。使用下面的代码对我来说效果很好

    /**
* Uplaod with Json Object
*
* @param _requestedURL
* @param obj
* @return
* @throws IOException
*/
public String UploadUrl(String _requestedURL, JSONObject obj)
throws IOException {
HttpPost _request = new HttpPost(_requestedURL);
HttpResponse _response = null;
DefaultHttpClient _httpClient = new DefaultHttpClient();
String _ContetnString = null;
try {
StringEntity se = new StringEntity(obj.toString());
_request.setEntity(se);
_request.setHeader("Accept", "application/octet-stream");
_request.setHeader("Content-type", "application/octet-stream");
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,
CommonValues.TIME_OUT);
HttpConnectionParams.setSoTimeout(httpParameters,
CommonValues.TIME_OUT);
_request.setParams(httpParameters);
_response = _httpClient.execute(_request);
if (_response != null
&& _response.getStatusLine().getStatusCode() == 200) {
//Read Responce
}
} catch (SocketException e) {
} catch (ConnectTimeoutException e) {
} catch (Exception e) {
}

return _ContetnString;
}

关于java - Android 如何将 JSON 对象作为 url 参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18357251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com