2016-08-05 74 views
1

我正在使用volley lib。在我的項目的每個活動中,我的第一個登錄頁面已經使用抽象的JSON對象請求方法(POST)完成,並且下一個活動無法分析。如何使用volley解析json數據

主屏幕的JSON數據:

{ 
"err-code": 5, 
    "job_details": [ 
    { 
     "job_id": 33, 
     "job_no": "ES53-AF", 
     "contract_manager_id": 4, 
     "company_name": "A Construction Ltd", 
     "time_spent": 4.5 
    }, 
    { 
     "job_id": 5, 
     "job_no": "ES1465-AF", 
     "contract_manager_id": 4, 
     "company_name": "Trios Property", 
     "job_description": "Carry out the rewire of ", 
     "time_spent": 26.5 
    }, 
    { 
     "job_id": 81, 
     "job_no": "ES101-AF", 
     "contract_manager_id": 4, 
     "company_name": "Arden Construction Ltd", 
     "job_description": "Carry out works as per esti 3AQ", 
     "time_spent": 2.5 
    }, 
    }] 
} 

代碼:

private void getDataNew() 
{ 
    String url = "http://103.5.103.8:067/ivservices/Webservices/joblisting"; 
    HashMap<String, String> user = session.getUserDetails(); 
    String token = user.get(SessionManager.KEY_TOKEN); 
    Log.e("token check kro", token); 
    final String role = user.get(SessionManager.KEY_ROLE); 
    Log.e("role", role); 
    String user_id = user.get(SessionManager.KEY_USERID); 
// Log.e("user_id", user_id.toString()); 
    Map<String, String> params = new HashMap(); 
    params.put(KEY_TOKEN, token); 
    params.put(KEY_ROLE, role); 
    params.put(KEY_ID, user_id); 

     JSONObject parameters = new JSONObject(params); 

     JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       // Log.e("response mila", response.toString()); 
       parseData(response); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) 
      { 
       error.printStackTrace(); 
       //TODO: handle failure 
      } 
     }); 

    Volley.newRequestQueue(this).add(jsonRequest); 
    } 
+0

[在Android中使用Volley解析JSON數據的可能的副本](http://stackoverflow.com/questions/18699988/parsing-data-from-json-using-volley-in-android) – Talha

回答

0

刪除來自你的json的Extra}。

您在json解析代碼時所做的一個錯誤您回答了@Er。 Arjun saini。

那樣的錯誤是在這條線:

JSONArray jsonArray = Object.getJSONArray( 「ERR-代碼」);

它應該是這樣的:

JSONArray jsonArray = Object.getJSONArray( 「job_details」);

確保JSON應該是這樣的:

{ 


"err-code": 5, 
    "job_details": [ 
    { 
     "job_id": 33, 
     "job_no": "ES53-AF", 
     "contract_manager_id": 4, 
     "company_name": "A Construction Ltd", 
     "time_spent": 4.5 
    }, 
    { 
     "job_id": 5, 
     "job_no": "ES1465-AF", 
     "contract_manager_id": 4, 
     "company_name": "Trios Property", 
     "job_description": "Carry out the rewire of ", 
     "time_spent": 26.5 
    }, 


{ 
    "job_id": 81, 
    "job_no": "ES101-AF", 
    "contract_manager_id": 4, 
    "company_name": "Arden Construction Ltd", 
    "job_description": "Carry out works as per esti 3AQ", 
    "time_spent": 2.5 
} 
    ] 
} 

,並嘗試這個代碼希望,如果不工作比請分享logcat的錯誤和異常

try { 
     JSONObject Object=new JSONObject(response); 
     int code = Object.getInt("err-code"); 
     if (code == 0) { 
      JSONArray jsonArray = Object.getJSONArray("job_details"); 
      List<JobList> jobListArray=new ArrayList<JobList>(); 
      for (int i = 0; i <= jsonArray.length(); i++) { 
       JSONObject jsonObject = (JSONObject) jsonArray.get(i); 
       JobList model_joblist=new JobList(); 
       model_joblist.setJobno(jsonObject.getString(JobList.JOB_NO)); 
       model_joblist.setJobid(jsonObject.getInt(JobList.JOB_ID)); 
       model_joblist.setDescreption(jsonObject.getString(JobList.JOB_DESCRIPTION)); 
       jobListArray.add(model_joblist); 
      } 
     } 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

如果仍然不能超過工作將工作在任何其他代碼中都有錯誤,例如在json是在你的model_joblist等

請分享更多的代碼和logcat。

+0

嗨..我有另一個問題...我如何才能實現我的Android應用程序..爲管理主屏幕不同和用戶主屏幕不同,除了登錄屏幕相同的應用程序 – harsh

+0

一個解決方案可能是: 您可以爲用戶和管理員製作不同的屏幕。 在登錄時檢查是否登錄管理員,然後將他移動到管理活動等....否則打開用戶活動..... 第二種解決方案,如果你有相同的屏幕,但不同的數據,然後檢查用戶或管理員負載後相關數據.... 試試哪個適合你..... –

0

試試這個

注意檢查還您JSON刪除,}前]比你有沒有語法錯誤響應

try { 

     int err_code=response.getInt("err-code"); 

     JSONArray jsonArray=response.getJSONArray("job_details"); 
     for(int i=0;i<jsonArray.length();i++) 
     { 
      JSONObject jsonObjectdetail=jsonArray.getJSONObject(i); 

      int job_id=jsonObjectdetail.getInt("job_id"); 
      int time_spent=jsonObjectdetail.getInt("time_spent"); 
      int contract_manager_id=jsonObjectdetail.getInt("contract_manager_id"); 
      String job_no=jsonObjectdetail.getString("job_no"); 
      String company_name=jsonObjectdetail.getString("company_name"); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

String job_no = jsonObjectdetail .getString( 「job_no」); 在這條線上沒有反應 – harsh

+0

對不起,改變response.getInt(「err-code」); ....查看更新回答,現在你得到 –

+0

之前有err_code所以沒有改變它作爲根據你的json –