5

指定されたjsonを文字列にラップし、androidでHttp putリクエストを介してサーバーに送信する方法は?

これは私のjsonがどのように見えるかです。

    {
    "version": "1.0.0",
    "datastreams": [
        {
            "id": "example",
            "current_value": "333"
        },
        {
            "id": "key",
            "current_value": "value"
        },
        {
            "id": "datastream",
            "current_value": "1337"
        }
    ]
}

上記は私のjson配列です。

以下は私がコードを書いた方法ですが、うまくいきません

        protected String doInBackground(Void... params) {
            String text = null;
            try {
                JSONObject child1 = new JSONObject();
                try{
                    child1.put("id", "LED");
                    child1.put("current_value", "0");


                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                JSONArray jsonArray = new JSONArray();

                jsonArray.put(child1);

                JSONObject datastreams = new JSONObject();
                datastreams.put("datastreams", jsonArray);  

                JSONObject version = new JSONObject();
                version.put("version", "1.0.0");
                version.put("version", datastreams);


             HttpClient httpClient = new DefaultHttpClient();
             HttpContext localContext = new BasicHttpContext();
             HttpPut put = new HttpPut("url");
             put.addHeader("X-Apikey","");
             StringEntity se = new StringEntity( version.toString());  
             se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

             put.addHeader("Accept", "application/json");
             put.addHeader("Content-type", "application/json");
             put.setEntity(se);


             try{

                   HttpResponse response = httpClient.execute(put, localContext);
                   HttpEntity entity = response.getEntity();
                   text = getASCIIContentFromEntity(entity);
             }
              catch (Exception e) {
                 return e.getLocalizedMessage();
             }


        }catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
            return text;
        }

これについて助けてください

4

5 に答える 5

4

これは1つのサンプルです。

    JSONObject Parent = new JSONObject();
    JSONArray array = new JSONArray();

    for (int i = 0 ; i < datastreamList.size() ; i++)
    {
        JSONObject jsonObj = new JSONObject();

        jsonObj.put("id", datastreamList.get(i).GetId());
        jsonObj.put("current_value", datastreamList.get(i).GetCurrentValue());
        array.put(jsonObj);
    }       
    Parent.put("datastreams", array);       
    Parent.put("version", version);

そしてそれを送るために:

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    StringEntity se = new StringEntity( Parent.toString());  
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    post.setHeader("Accept", "application/json");
    post.setHeader("Content-type", "application/json");
    post.setEntity(se);
    client.execute(post);

編集

forステートメントで使用されるこのサンプルdatastreamListでは、​​サーバーに送信するすべての値に対して必要なリストです(2つのプロパティを持つ1つのクラスの1つのリスト、 idおよびvalue)、実際には次のような2つのクラスがあると思います:

class A {

List<Datastreams> datastreamList
String version;
//get
//set
}

class Datastreams {

String id;
String current_value; // or int
//get
//set
}

コードには、サーバーに送信する A クラスのオブジェクトが1つ必要なので、最初の部分を使用して to にマップできobjectますjson

于 2014-01-27T09:31:02.790 に答える
0

String として送信する必要があるため、次の JSON データを String に格納する必要があります

{
    "version": "1.0.0",
    "datastreams": [
        {
            "id": "example",
            "current_value": "333"
        },
        {
            "id": "key",
            "current_value": "value"
        },
        {
            "id": "datastream",
            "current_value": "1337"
        }
    ]
}

次に、次のように送信する必要があります。

pairs.add(new BasicNameValuePair("data", finalJsonObject.toString()));
于 2014-01-27T09:59:37.283 に答える
0

ここにAndroidクライアントライブラリが役立ちます:Httpzoid - Android REST(JSON)クライアント、いくつかの例があり、投稿を投稿したり、リクエストを簡単に取得したりできます。 https://github.com/kodart/Httpzoid

于 2016-01-28T07:30:54.433 に答える