2017-08-22 49 views
-1

我是Android的学习的初学者,我用改装在我的项目,我得到的登录请求响应如何从改造Android的

{"android":{"msg":"User login successfull","data":{"id":"14","name":"meena","url":"","email":"[email protected]","password":"202cb962ac59075b964b07152d234b70","phone":"","country":"China","photo":"","status":"pending","role":"3","device_id":"","oauth_uid":"","resume":"","oauth_provider":null,"created":"1503316508"}}} 
+1

你的问题是什么? –

+0

LoginResponse getResult = response.body(); Log.i(“getResult”,“”+ getResult);我无法打印响应值,我将在哪里得到 –

+2

解析解析解析!解析它 – Anurag

回答

2

试试这个你可以分析你的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(); 
} 
+0

如何获取“JsonResponseString”,如果我使用打印值,使用\t LoginResponse getResult = response.body(); Log.i( “的getResult”, “” +的getResult);的getResult:[email protected] –

+0

这是你从改造API –

+0

等待让我会在你们的帮助代码和更新您检查输入反应 –

0

您可以使用GSON此:

第一,创建一个使用http://pojo.sodhanalibrary.com/

一个POJO

让我们命名它响应类

现在使用GSON如下:

Gson gson = new Gson(); 
         Response Response = gson. 
           fromJson(response.toString(), Response.class); 

最好方法: 1.使用改型GSON库编译

'com.squareup.retrofit2:converter-gson:2.2.0' 
  • 添加GsonFactory进行改造

    new Retrofit.Builder() .baseUrl(Common.Urls.BaseURL) .addConverterFactory(GsonConverterFactory.create());

  • 创建POJO如上

  • 观察改型响应作为一类对象:

    @POST(Common.Urls.LOGIN_API) 可观察登入(@Body HashMap的请求);