1

私のアプリケーションでは、UserIdなどのパラメーターを使用してJSONObjectを送信しようとしています。誰かが私にいくつかのチュートリアルを提案してくれますか?

このコードを取得しましたが、このコードでは、パラメーターではなくJSONObjectのみを送信できます。

私のURLは次のようなものです:http://www.example.com/JSONObject/userId

userIdを持つJSONObjectをサーバーに渡すにはどうすればよいですか。

public class HTTPPoster {
    public HttpResponse doPost(String url, JSONObject jsonProfilo) throws ClientProtocolException, IOException {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost request = new HttpPost(url);
        HttpEntity entity;
        StringEntity s = new StringEntity(jsonProfilo.toString());
        s.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        entity = s;
        request.setEntity(entity);
        HttpResponse response;
        response = httpclient.execute(request);
        return response;
    }
}

ありがとう..

4

2 に答える 2

0

JSONObjectでは、次のようなパラメータを保持できます。

JSONObject jsonvalue =new JSONObject();
jsonvalue.put("userId", "urUserId");

サンプルコード、ここではサーバーimeiNumber、userName、paaswordを送信しています。

JSONObject jsonvalue =new JSONObject();
jsonvalue = new JSONObject();
jsonvalue.put("imeiNumber", "00000000000");
jsonvalue.put("userName", mUserName);
jsonvalue.put("plainPassword", mPassword);

私のポイントは、同様に、userIdとglobalIdをサーバーに送信できるということでした。

今urHttpConnectionで:

HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setChunkedStreamingMode(0);
conn.setUseCaches(false);
String urlParameters = "body"
    + URLEncoder.encode(jsonvalue.toString(), "UTF-8");       
conn.setRequestProperty("Content-Length",
    "" + Integer.toString(urlParameters.getBytes().length));

try {
     OutputStream os = conn.getOutputStream();
     os.write(jsonvalue.toString().getBytes());
     os.close();
} catch (IOException e) {
    e.printStackTrace();
}
于 2013-02-19T09:32:18.690 に答える
0

「パラメーターではなく、JSONObject のみを送信できます。」とはどういう意味ですか? 、すべてのパラメーターを JsonObject 自体に入れることができますが、実際の問題が何であるかわかりません

于 2013-02-19T09:29:32.570 に答える