1

Android から残りの Web サービスに投稿したい Payload オブジェクトがあります。

これが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 サービスに送信することです。

4

1 に答える 1

0

json ライブラリを使用して、json を REST サービスにポストします。

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 に答える