2017-12-22 12 views
0

안녕 친구 나는 학생 데이터베이스 관리 시스템에 대한 응용 프로그램을 개발하고 있습니다. 다음은 각 사용자 서비스 요청에 대해 서버에 요청하는 코드이며 서버는 Json에서와 같이 응답을 보냅니다. 울부 짖는 소리 같이Google Volley 요청을 반복 작성하는 대신 한 번 쓰는 방법은 무엇입니까?

LoginActivity.java

 onCreate(){ 

       requestQueue = Volley.newRequestQueue(context); 

       //making service method call here 
       JSONObject json = makeRequest(url,loginJson); 

       //printing response given by makeRequest() method 
       Log.e("std","-----------------Inside onCreate()----------------------"); 
       Log.e("std"," requested json "+json); 
     } 

    ////// 

    public JSONObject makeRequest(String url, JSONObject jsonObject){ 

    JSONObject myjson; 

    JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 

      Log.e("std","-----------------Inside onResponse()----------------------"); 
      Log.e("std","response \n"+response); 

      myJson = new JSONObject(String.valueOf(response)) 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      //This code is executed if there is an error. 
      error.printStackTrace(); 
      Log.e("error"," onErrorResponse "+error); 
      Toast.makeText(context,"Server Error...",Toast.LENGTH_SHORT).show(); 
     } 
    }){ 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      headers.put("Content-Type", "application/json; charset=utf-8"); 
      return headers; 
     } 

    }; 

    requestQueue.add(myReq); 


    return myJson; 
} 

는 onResponse 리스너 내부에 난 내 반응을 얻고있다. 하지만 makeRequest 메서드가 null을 반환합니다 JSON.

로그 캣

com.example.stddbsystem E/std:-----------------Inside onResponse()---------------------- 
    com.example.stddbsystem E/std:{"responseCode": 200,"responseMessage": "Login Success","userId": 1} 

    com.example.stddbsystem E/std:-----------------Inside onCreate()---------------------- 
    com.example.stddbsystem E/std:requested json : null 

답변

0

이 시도 :

변경하려면 다음

@Override 
    public void onResponse(JSONObject response) { 

     myJson =response; 

    } 

대신에 :

 @Override 
    public void onResponse(JSONObject response) { 


    myJson = new JSONObject(String.valueOf(response)) 

    }