【问题标题】:Using the response from Http get json使用来自 Http 的响应获取 json
【发布时间】:2014-09-23 18:15:09
【问题描述】:

使用下面的代码,我可以从 login/json 中获取 json 信息。

如何将其转换为我可以使用的东西? 我现在正在尝试确定用户是否存在。 如果没有返回:

 `{
    "user": null,
    "sessionId": null,
    "message": "Invalid email/username and password"
}

任何指导都会很棒。 `

        HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        HttpPost httpPost = new HttpPost("http://localhost:9000/auth/login/json");
         // Building post parameters
        // key and value pair

        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
        nameValuePair.add(new BasicNameValuePair("user", "user"));
        nameValuePair.add(new BasicNameValuePair("pw", "password"));

        // Url Encoding the POST parameters
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }

        // Making HTTP Request
        try {

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            // writing response to log
            Log.d("Http Response:", response.toString());
            System.out.println(EntityUtils.toString(entity));

        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();

        }

【问题讨论】:

    标签: android json http


    【解决方案1】:
    You can use google GSON to map the json to you model.
    
    or simply
    
    JSONObject obj = new JSONObject(jsonresponse.toString());
    
    String user = obj.optString("user",null);
    
    In this way you can access the response.
    
    if(user == null){
      // not authorised or login
    }
    

    【讨论】:

    • 我将如何使用该空值?我可以通过 if(user=null) 来使用它吗?
    【解决方案2】:

    第一:

        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
        nameValuePair.add(new BasicNameValuePair("user", "user"));
        nameValuePair.add(new BasicNameValuePair("pw", "password"));
    
        // Url Encoding the POST parameters
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        }
    

    不正确,使用下一个:

      JSONObject body = new JSONObject();
      body.put("user", "user");
      body.put("pw", "password");
      String strBody = body.toString();
    
      httpPost.setEntity(new StringEntity(strBody, "UTF-8"));
    

    解析 json 字符串可以使用 gson 库,对我来说非常好。只需创建一个响应模型,例如:

       public class SessionResponse implements Serializable{
         @SerializedName("user")
         private User user;
    
         @SerializedName("sessionId")
         private String sessionId;
    
         @SerializedName("message")
         private String message;
    
       }
    

    然后就用下一个:

       String yourResponse = EntityUtils.toString(entity);
       SessionResponse sessionResponse = new Gson().fromJSON(yourResponse, SessionResponse.class);
    

    现在你有了 SessionResponse 的对象并且可以用它做任何事情。请注意,您要转换的每个类都应标记为“可序列化”并实现可序列化接口。

    【讨论】:

    • 为什么要序列化模型?
    猜你喜欢
    • 2022-06-28
    • 2012-11-06
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多