1

我有一个 Payload 对象,我想将它从 android 发布到 rest webservice:

这是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 发送到一个休息网络服务。

4

1 回答 1

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;
}
于 2013-07-14T12:47:21.070 回答