如何将给定的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;
}
请帮助解决此问题
答案 0 :(得分:4)
这是一个样本。
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个属性,id
和{{ 1}}),实际上我认为你有两个类似下面的那个:
value
并且在您的代码中,您必须有一个A类对象要发送到服务器,因此您可以使用第一部分将class A {
List<Datastreams> datastreamList
String version;
//get
//set
}
class Datastreams {
String id;
String current_value; // or int
//get
//set
}
映射到object
。
答案 1 :(得分:2)
如果您更喜欢使用图书馆,那么我希望您使用Kaush Ion Library。
从这个库中构建您只需发布您的JSON:
JsonObject json = new JsonObject();
json.addProperty("foo", "bar");
Ion.with(context, "http://example.com/post")
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// do stuff with the result or error
}
});
答案 2 :(得分:0)
只需要发送一个String,然后按照String
中的JSON数据存储{
"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()));
答案 3 :(得分:0)
'{'括号代表一个对象,'['代表一个数组或列表。在你的情况下创建一个bean
YourObj{
private String version;
private List<DataStream> datastreams;
//getters
//setters
}
DataStream{
private String id;
private String current_value;
//getters
//setters
}
使用org.codehaus.jackson:jackson-xc
jar进行json解析
使用ObjectMapper
字符串到对象
YourObj obj = new ObjectMapper().readValue(stringyouwanttopass,new TypeReference<YourObj>(){});
现在您可以使用已解析的值。
或者您可以将值设置为YourObj
YourObj obj =new YourObj();
obj.setVersion(1.0.0);
List<Datastream> datastreams=new ArrayList<Datastream>();
Datastream datestr=new Datastream();
datestr.setId("example");
datestr.setCurrent_value("333");
datastreams.add(datestr);
datestr.setId("key");
datestr.setCurrent_value("value");
datastreams.add(datestr);
datestr.setId("datastream");
datestr.setCurrent_value("1337");
datastreams.add(datestr);
JSONObject jsonget = new JSONObject(appObject);
jsonget.toString();
使用Jersey连接服务器
Client client = Client.create();
WebResource webResource = client.resource("serverURl");
ClientResponse response = webResource.path("somePath")
.type("application/json").accept("application/json")
.post(ClientResponse.class, jsonget.toString());
在服务器端将其作为字符串并解析它。
答案 4 :(得分:0)
这里有一个android客户端库可以帮到你: Httpzoid - Android REST(JSON)客户端,它有一些例子,你可以做贴,轻松获取请求。 https://github.com/kodart/Httpzoid