我有一个Payload对象,我想将其发布到其他Web服务 来自android:
这是json有效载荷
{
"dataSource": "954b9e59-eba0-4679-afbb-2878580c054d",
"discriminator": "encounter",
"payload": {
"person": {
"person.uuid": "269857b6-f0b9-4c93-af4a-f18293679e89"
},
"encounter": {
"form.uuid": "fe9fc58f-d196-46cb-956d-f46445f558d9",
"encounterType.uuid": "bdb58960-4d91-4ca7-a27a-8dabde40ec12",
"provider.uuid": "3c023ab8-82ff-11e2-96ef-f0def12f7061",
"location.uuid": "e1b1d9eb-69a2-4843-bccc-e73e33c70e4c",
"datetime": "2013-06-17 00:00:00"
},
"obs": [
{
"uuid": "307AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"type": "coded",
"value": "1364AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
}
]
}
}
并在此处定义了有效负载对象: http://pastebin.com/yvaY0Q6r
我的主要目标是在android中创建一个接口和活动,以接收必要的数据并将其作为json发送到其他Web服务。
答案 0 :(得分:0)
使用jacson库将json发布到休息服务。
public String addPerson() {
HttpClient client = new DefaultHttpClient();
String request_string = null;
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
HttpResponse response;
try {
HttpPost post = new HttpPost(ServiceConstant.ACTIVATElICENCE);
Log.e("url", ""+ServiceConstant.ACTIVATElICENCE);
post.setHeader("Content-type", "application/x-www-form-urlencoded");
ActivationBean personactivationdata = new ActivationBean();
try {
personactivationdata.setPersonID(PersonId);
personactivationdata.setActivationKey(ActivationKey);
personactivationdata.setDeviceID(MyPreferences.getInstance(
sContext).getDeviceId());
personactivationdata.setRegistrationID(MyPreferences
.getInstance(sContext).getDeviceRegistrationId());
request_string = convertBeanToJson(personactivationdata);
} catch (Exception e) {
}
request_string = request_string.replace("\\", "")
.replace("\"[", "[").replace("]\"", "]");
request_string = request_string.replace("\n", "").replace("\r", "");
Log.e("req string for activation", "" + request_string);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair(
ServiceConstant.RequestData, request_string));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
response = client.execute(post);
/* Checking response */
if (response != null) {
InputStream inputStream = response.getEntity().getContent(); // Get
request_string = convertStreamToString(inputStream);
Log.e("response",
"response of post = " + request_string.toString());
return request_string;
}
} catch (IOException e) {
request_string = "timeout";
e.printStackTrace();
} catch (Exception e) {
// Toast.makeText(sContext, "Server Error",
// Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return request_string;
}
private String convertBeanToJson(ActivationBean bean) {
String reqQuery = null;
try {
reqQuery = mapperObj.writeValueAsString(bean);
Log.d("convertBeanToJson(): request query is:", reqQuery);
} catch (JsonGenerationException e) {
Log.e("convertBeanToJson():", " caught JsonGenerationException");
} catch (JsonMappingException e) {
// mELogger.error("convertBeanToJson(): caught JsonMappingException");
} catch (IOException e) {
Log.e("convertBeanToJson():", " caught IOException");
}
return reqQuery;
}