zoukankan      html  css  js  c++  java
  • android使用JSON数据和服务器进行交互

    //点击按钮发送反馈信息给服务端,成功则进入优惠券界面
    
            Button upload = (Button) findViewById(R.id.upload);
            final String finalLatitude = latitude;
            final String finalLongitude = longitude;
            upload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    //获得建筑物名称
                    building=(EditText) findViewById(R.id.buildingInput);
                    final String buildingInput = building.getText().toString();
    
                    //获得楼层号
                    floor = (EditText)findViewById(R.id.floorInput);
                    final String floorInput = floor.getText().toString();
    
                    //获得手机号
                    telephone=(EditText)findViewById(R.id.telInput);
                    final String telInput = telephone.getText().toString();
    
                    //获得描述
                    description=(EditText)findViewById(R.id.descripInput);
                    final String descripInput = description.getText().toString();
    
                    if (buildingInput.length() <= 0
                            || floorInput.length() <= 0
                            || telInput.length() <= 0
                            || descripInput.length() <= 0)
                    {
                        Toast.makeText(Feedback.this, "请完成反馈信息的填写", 
    
    Toast.LENGTH_LONG).show();
                    }
                    else{
                        new Thread() {
                            public void run() {
                                Looper.prepare();
                                final String urlPath = "http://10.8.176.105:8080/QRCodeAdmin
    
    /commAction.action";
                                URL url;
    
                                try {
                                    url = new URL(urlPath);
    
                                    JSONObject feedbackInfo = new JSONObject();
                                    feedbackInfo.put("building", buildingInput);
                                    feedbackInfo.put("floor", floorInput);
                                    feedbackInfo.put("telephone", telInput);
                                    feedbackInfo.put("description", descripInput);
                                    feedbackInfo.put("latitude", finalLatitude);
                                    feedbackInfo.put("longitude", finalLongitude);
    
                                    /**
                                     *
                                     * 封装feedback数组
                                     使用JsonObject封装
                                     {"building":"","floor":"","telephone":"","description":"",
                                     "latitude":"","longitude":""}
                                     */
                                    //将JSON数组转换成String类型使用输出流向服务器写
                                    String content = String.valueOf(feedbackInfo);
    
                                    //输出
                                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                                    conn.setConnectTimeout(5000);
                                    conn.setDoOutput(true);//允许输出
                                    conn.setRequestMethod("POST");//post方式输出
                                    conn.setRequestProperty("Content-Type", "application/json");
                                    OutputStream os = conn.getOutputStream();
                                    os.write(content.getBytes());
                                    os.close();
    
                                    if (conn.getResponseCode() == 200) {
                                        //传回信息,信息应当为优惠券的信息
                                        InputStream is = conn.getInputStream();
    
                                        //下面的Json数据是{"id":"","path":""}的string形式
                                        String json = NetUtils.readString(is);
                                        awardBean award = new awardBean();
                                        JSONObject jsonObject = new JSONObject(json);
    
                                        //获得award的id和path
                                        String id = jsonObject.getString("id");
                                        String path = jsonObject.getString("path");
    
                                        //将获得的优惠券信息发送给award界面
                                        Intent intent1 = new Intent(Feedback.this, Award.class);
                                        intent1.putExtra("id", id);
                                        intent1.putExtra("path", path);
                                        startActivity(intent1);
                                    } else {
                                        Toast.makeText(Feedback.this, "发送失败", 
    
    Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    Toast.makeText(Feedback.this, "发送失败",
    
     Toast.LENGTH_SHORT).show();
                                } catch (MalformedURLException e) {
                                    Toast.makeText(Feedback.this, "发送失败", 
    
    Toast.LENGTH_SHORT).show();
                                } catch (IOException e) {
                                    Toast.makeText(Feedback.this, "发送失败", 
    
    Toast.LENGTH_SHORT).show();
                                }
                            }
                        }.start();
                    }
                }
    
            });

    下面是awardBean(我也不知道这个bean写的是不是必要):

    package com.example.euphemiaxiao.feedback;
    
    import java.io.Serializable;
    /**
     * Created by Euphemia Xiao on 2017/7/13.
     */
    
    public class awardBean implements Serializable{
        public String path;
        public String id;
    
        public awardBean()
        {
            this.path = path;
            this.id=id;
        }
        public String getPath()
        {
            return path;
        }
    
        public String getId()
        {
            return id;
        }
    }

    NetUtils是一个工具类:

    package com.example.euphemiaxiao.feedback;
    
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    /**
     * Created by Euphemia Xiao on 2017/7/4.
     */
    
    public class NetUtils {
        public static byte[] readBytes(InputStream is){
            try {
                byte[] buffer = new byte[1024];
                int len = -1 ;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                while((len = is.read(buffer)) != -1){
                    baos.write(buffer, 0, len);
                }
                baos.close();
                return baos.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null ;
        }
        public static String readString(InputStream is){
            return new String(readBytes(is));
        }
    }

    P.S.另一种接收(上面的接收是接收一条信息{"award":{"id":"","path":""}},下面的接收是接收一组信息{{"student":{"studentID":"","name":""}}{"student":{"studentID":"","name":""}}{"student":{"studentID":"","name":""}}...})

    JSONArray actArray = jsonObject.getJSONArray("student");
    
                    for (int i = 0; i < actArray.length(); i++) {
                        //获得JSON数组中的每一个JSONObject对象
                        JSONObject actObject = actArray.getJSONObject(i);
                        String stuID=actObject.getString("studentID");
                        studentID.add(stuID);
                        String stuName = actObject.getString("name");
                        //studentName.add(stuName);
                        studentInfo.add(stuID+" "+stuName);
                    }

    再P.S.获得一个json数据,前端对其进行处理,获取key和value

    var obj = {"abc":"123"};
    for(var key in obj) {
    console.log(key+"+"obj[key])}

    输出结果为abc+123

  • 相关阅读:
    The Dos and Don'ts for Ceph for OpenStack
    fio测试ceph的filestore
    yum安装Ceph指定Jewel版本
    处理stale的pg
    预估Ceph集群恢复时间
    python编码(二)
    python编码(一)
    删除重复的feature vba VS 删除重复的feature python
    新浪微博mid和url的互算
    用python实现各种排序算法
  • 原文地址:https://www.cnblogs.com/xym4869/p/8477656.html
Copyright © 2011-2022 走看看