繁体   English   中英

如何从 http 请求中获取 json 响应?

[英]How to obtain json reponse from http request?

当试图从我的 api 检索响应 json 时,我只能得到一个我无法转换为 jsonobject 使用的字符串值。

应以 json 格式 {"result":"success","client[id]":"1"} 响应

但我只能检索字符串“result=success,client[id]=1”

public void createWHMCSUser(final String emailID, final String random, final String uid) {

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String e = URLEncoder.encode(emailID, "utf-8");
                String r = URLEncoder.encode(random, "utf-8");
                URL url = new URL("http://create.user.com/includes/api.php?action=AddClient&username=abc123&password=abc123&accesskey=abc123&firstname=User&lastname=Name&email=" + e +"&address1=na&city=na&state=na&poscode=00000&country=US&phonenumber=0000000000&password2=" + r + "&reponsetype=json");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Accept","application/json");
                conn.setDoOutput(true);
                conn.setDoInput(true);



                Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                Log.i("MSG", conn.getResponseMessage());

                if(conn.getResponseCode() == 200){
                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    String readAPIResponse = " ";
                    StringBuilder jsonString = new StringBuilder();
                    while((readAPIResponse = in.readLine()) != null){
                        jsonString.append(readAPIResponse);

                        JSONObject obj = new JSONObject(jsonString.toString());
                        int aJsonString = obj.getInt("Client[id]");
                        SetClientID(uid,aJsonString);



                    }

                    in.close();


                }

                conn.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });

    thread.start();

}

使用这种方式更容易和更快地获得更多示例并构建库检查:在此处输入链接描述

 AndroidNetworking.get("URL")
                     .setTag("test")
                     .setPriority(Priority.MEDIUM)
                     .build()
                     .getAsJSONObject(new JSONObjectRequestListener() {
                        @Override
                        public void onResponse(JSONObject response) {
                          // handle your response here 

                        }
                        @Override
                        public void onError(ANError error) {
                          // handle error
                        }
                    });

使用 json 库将 java object 添加到 JSONObject。 这会将所有 java object 内容存储为 json 字符串格式的服务器代码。 这会将 In 转换为 json 格式。

然后,您将从服务器返回代码作为常规字符串。

在客户端,您将收到它作为字符串。

然后你需要将它从 json 字符串转换为 java object 。 那是实际的 json 字符串。 您将能够将该字符串转换为 java obj。

但是您返回的不是 json 字符串。 那只是字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM