Android给服务器发送json数据初体验

本文介绍了一个具体的Android应用程序中如何使用HTTPURLConnection发起POST请求的例子。该示例展示了如何构造JSON请求体、设置请求头、发送请求并处理服务器响应。
new Thread()
        {
            @Override
            public void run() {
            // TODO Auto-generated method stub
                Looper.prepare();
                final String urlPath="http://rc-ws.mymhotel.com/hotelservice/checkin/service/add_task.do";
                URL url;
                try
                {
                    url = new URL(urlPath);
                    /*下面是发送给服务器的json文本*/
                    JSONObject jsonObj = new JSONObject();
                    jsonObj.put("diviceId", "CDSY00051")
                            .put("location", "速八酒店")
                            .put("locationArea", "铜锣湾红星社团")
                            .put("timeLimit", "2015-11-30 10:11:29")
                            .put("priority", 0)
                            .put("patternInfo", 1)
                            .put("category", "0")
                            .put("messageInfo", "三斤牛肉")
                            .put("locationDesc", "商务中心301");
                  /*JSON数据转换成String类型使用输出流向服务器写*/
                  /*格式为:sendcontent={"diviceId":"CDSY00051","location":"速八酒店","locationArea":"铜锣湾红星社团",
                  "timeLimit":"2015-11-30 16:58:29","priority":0,"patternInfo":1,"category":"0","messageInfo":"三斤牛肉",
                  "locationDesc":"商务中心301"}
                   */
//                String sendcontent = String.valueOf(jsonObj);
                  String sendcontent = jsonObj.toString();
                  Log.d("CallServiceActivity", "sendcontent=" + sendcontent);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setDoOutput(true);//设置允许输出
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("User-Agent", "Fiddler");
                    conn.setRequestProperty("Content-Type", "application/json");
                    conn.setRequestProperty("Charset", "utf-8");
                    OutputStream os = conn.getOutputStream();
                    os.write(sendcontent.getBytes());
                    os.close();
                    /*服务器返回的响应码*/
                    int code = conn.getResponseCode();
                    if(code == 200)
                    {
                        //这里接收服务器返回的数据(如:responseData={"taskCode":"s2015113010041049003"}                        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
                        String retData = null;
                        String responseData = "";
                        while((retData = in.readLine()) != null)
                        {
                            responseData += retData;
                        }
                        JSONObject jsonObject = new JSONObject(responseData);
                        String success = "发送成功啦";
                        //打印输出服务器返回的数据
                        Log.d("CallServiceActivity","成功啦responseData="+responseData);
                        in.close();
                        //System.out.println(success);
                        Toast.makeText(CallServiceActivity.this, success, Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "数据提交失败", Toast.LENGTH_SHORT).show();
                    }
                }
                catch (Exception e)
                {
                // TODO: handle exception
                    throw new RuntimeException(e);
                }
                Looper.loop();
            }

        }.start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值