Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从改型android获取响应值(登录响应)_Android_Retrofit2 - Fatal编程技术网

如何从改型android获取响应值(登录响应)

如何从改型android获取响应值(登录响应),android,retrofit2,Android,Retrofit2,我是Android初学者,我在我的项目中使用了改型,我从登录请求中得到响应 {"android":{"msg":"User login successfull","data":{"id":"14","name":"meena","url":"","email":"meena04@gmail.com","password":"202cb962ac59075b964b07152d234b70","phone":"","country":"China","photo":"","status":"pen

我是Android初学者,我在我的项目中使用了改型,我从登录请求中得到响应

{"android":{"msg":"User login successfull","data":{"id":"14","name":"meena","url":"","email":"meena04@gmail.com","password":"202cb962ac59075b964b07152d234b70","phone":"","country":"China","photo":"","status":"pending","role":"3","device_id":"","oauth_uid":"","resume":"","oauth_provider":null,"created":"1503316508"}}}

试试这个,你可以像这样解析你的json

try
{

  JSONObject json = new JSONObject(jsonResponse);
  String msg= data.getString("msg")// get your msg

  JSONObject data = json.getJSONObject("data");// now get your secong json object like this

   String id= data.getString("id");// get the id from data json object like this

   String name= data.getString("name");// get the name from data json object like this

   String url= data.getString("url");// get the url from data json object like this

   String email= data.getString("email");// get the email from data json object like this

   String password= data.getString("password");// get the password from data json object like this

   String phone= data.getString("phone") ; // get the phone from data json object like this

   String country= data.getString("country");// get the country from data json object like this

   String photo= data.getString("photo");// get the photo from data json object like this

   String status= data.getString("status");// get the status from data json object like this

   String role= data.getString("role");// get the role from data json object like this

   String device_id= data.getString("device_id");// get the device_id from data json object like this

   String oauth_uid= data.getString("oauth_uid");// get the oauth_id from data json object like this

   String resume= data.getString("resume");// get the resune from data json object like this

   String oauth_provider= data.getString("oauth_provider");// get the oauth_provider from data json object like this

   String created= data.getString("created");// get the created from data json object like this

    Log.e("RESULT :-> ",msg+" "+id+""+name+"  "+url+""+email+""+password+" "+phone+" "+phone
    +country+" "+photo+""+status+" "+role+" "+device_id+" "+resume+"  "+oauth_provider+" "+created+ " ");

 } catch (JSONException e) {
   e.printStackTrace();
}

您可以使用Gson进行以下操作:

首先,使用

让我们把它命名为响应类

现在按如下方式使用gson:

Gson gson = new Gson();
                        Response Response = gson.
                                fromJson(response.toString(), Response.class);
最佳方法: 1.使用改进的Gson库编译

'com.squareup.retrofit2:converter-gson:2.2.0'
  • 将GsonFactory添加到改装中

    新的改型.Builder() .baseUrl(Common.url.baseUrl) .addConverterFactory(GsonConverterFactory.create())

  • 如上所述创建pojo

  • 将改装响应作为类对象进行观察:

    @POST(Common.url.LOGIN\u API) 可观测信号(@Body HashMap请求)


  • 您的问题是什么?LoginResponse getResult=response.body();Log.i(“getResult”,即“+getResult”);我无法打印响应值,我将在那里获取解析!!!如果我使用LoginResponse getResult=response.body()打印值,则解析如何获取“JsonResponseString”;Log.i(“getResult”,即“+getResult”);getResult:com.app.eduxhub.Model。LoginResponse@556defbit是您从改装apiwait获得的响应吗?我将检查您的帮助代码并向您更新我的问题是我无法在日志中打印响应,{“android”:{“msg”:“用户登录成功”,“数据”:{“id”:“14”,“姓名”:“meena”,“url”:“电子邮件”:meena04@gmail.com“,“密码”:“202cb962ac59075b964b07152d234b70”,“电话”:“国家”:“中国”,“照片”:“状态”:“待定”,“角色”:“3”,“设备id”:“oauth_uid”:“恢复”:“oauth_提供程序”:null,“创建”:“1503316508”}}}使用上述代码,您可以打印该响应@SelvaMeena