android 服务器怎么返回json数据格式,如何将Android中的数据以JSON格式发布到服务器?...

我自己做的。

JSONObject returnedJObject= new JSONObject();

JSONObject KeyvalspairJObject=new JSONObject ();

JSONObject devcKeyvalspairJObject=new JSONObject ();

JSONObject capabilityJObject=new JSONObject();

JSONObject ScreenDimensionsJObject =new JSONObject();

JSONObject deviceJObject= new JSONObject();

try{

KeyvalspairJObject.put("key1","val1");

KeyvalspairJObject.put("key2","val2");

capabilityJObject.put("sms", false);

capabilityJObject.put("data", true);

capabilityJObject.put("gps", true);

capabilityJObject.put("wifi", true);

capabilityJObject.put("keyValue", KeyvalspairJObject);

ScreenDimensionsJObject.put("width", 45);

ScreenDimensionsJObject.put("height", 45);

devcKeyvalspairJObject.put("Devckey1","val1");

devcKeyvalspairJObject.put("DEVCkey2","val2");

deviceJObject.put("userAgent", "MYUserAgent");

deviceJObject.put("capabilities", capabilityJObject);

deviceJObject.put("screen", ScreenDimensionsJObject);

deviceJObject.put("keyValue", devcKeyvalspairJObject);

returnedJObject.put("clientId", "ID:1234-1234");

returnedJObject.put("carrier","TMobile");

returnedJObject.put("device",deviceJObject);

returnedJObject.put("time",1294617435);

returnedJObject.put("msisdn","1234567890");

returnedJObject.put("timezone","GMT");

}

catch(JSONException e)

{

}

这就是我们如何将JSON数据发送到服务器。

public String putDataToServer(String url,JSONObject returnedJObject) throws Throwable

{

HttpPost request = new HttpPost(url);

JSONStringer json = new JSONStringer();

StringBuilder sb=new StringBuilder();

if (returnedJObject!=null)

{

Iterator itKeys = returnedJObject.keys();

if(itKeys.hasNext())

json.object();

while (itKeys.hasNext())

{

String k=itKeys.next();

json.key(k).value(returnedJObject.get(k));

Log.e("keys "+k,"value "+returnedJObject.get(k).toString());

}

}

json.endObject();

StringEntity entity = new StringEntity(json.toString());

entity.setContentType("application/json;charset=UTF-8");

entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));

request.setHeader("Accept", "application/json");

request.setEntity(entity);

HttpResponse response =null;

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpConnectionParams.setSoTimeout(httpClient.getParams(), Constants.ANDROID_CONNECTION_TIMEOUT*1000);

HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),Constants.ANDROID_CONNECTION_TIMEOUT*1000);

try{

response = httpClient.execute(request);

}

catch(SocketException se)

{

Log.e("SocketException", se+"");

throw se;

}

InputStream in = response.getEntity().getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String line = null;

while((line = reader.readLine()) != null){

sb.append(line);

}

return sb.toString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值