【问题标题】:How to pass string in json object?如何在json对象中传递字符串?
【发布时间】:2016-12-28 05:16:50
【问题描述】:

我是安卓开发新手!

我正在尝试在 json 对象中传递字符串

代码:

 String URL="http://some ip address"; //Here i have to call clientInfo

 protected JSONObject doInBackground(String... args) {


        String no_of_fishes = args[3];
        String no_of_alarms = args[2];
        String password = args[1];
        String username= args[0];

        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username", username));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("no_of_alarms", no_of_alarms));
        params.add(new BasicNameValuePair("no_of_fishes", no_of_fishes));

        JSONObject json = jsonParser.makeHttpRequest(URL, "POST", params);

        return json;

    }


public static ArrayList<String> getClientList() {
    ArrayList<String> clientList = new ArrayList<>();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] clientInfo = line.split(" +");
            String mac = clientInfo[3];
            if (mac.matches("..:..:..:..:..:..")) {
                clientList.add(clientInfo[0]);
                ipadd.setText("Client Ip:" + clientInfo[0]);
            }
        }
    } catch (java.io.IOException aE) {
        aE.printStackTrace();
        return null;
    }
    return clientList;
}

如何在字符串 URL 中调用 clintInfo,我是 java 和 android 新手,请任何人帮助如何做到这一点。

【问题讨论】:

标签: java android json object


【解决方案1】:

您可以使用以下代码将 clientInfo 数组转换为 JSON

JSONObject jsonobj= new JSONObject();//create json object
jsonobj.put("name", new JSONArray(clientInfo[]));

【讨论】:

    【解决方案2】:

    如果我理解正确,这就是你想要的

    JSONArray clients= new JSONArray(getClientList());
    JSONObject res = new JSONObject();
    res.put("clientlist",clients);
    

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多