2017-05-23 5 views
0

이 시점에서해야 할 일은 연결 버튼을 눌러 잠금 요청을 허용하는 간단한 앱입니다. 그것이 부여되면 JSON 응답이 나타납니다. 이것은 strings.xml에 붙여 넣은 HTTP 헤더 (X-API-key)가 필요합니다. 헤더에 지정했습니다. 나는 매개 변수도 포함시켰다. 나는 이미 우체부에서 URL을 테스트했으며 정상적으로 작동합니다. 여기에 오류 메시지가 나타납니다. 아래에서 볼 수 있듯이, 메인 활동과 MySingleton의 두 클래스가 있습니다. 미리 감사드립니다.Android Volley Json 요청 : 매개 변수가 표시되지 않습니다.

public class MainActivity extends AppCompatActivity { 

    private Button connect; 
    private ImageView mImageView; 
    private TextView textResponse; 
    private Map<String, String> headers; 
    private Map<String, String> mParams; 

    String lockUrl = "https://lzx650r9ih.execute-api.us-west-2.amazonaws.com/p01/lock"; 


    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mImageView = (ImageView) findViewById(R.id.imageView); 
     connect = (Button) findViewById(R.id.connect_button); 
     textResponse = (TextView) findViewById(R.id.textResponse); 

     //press the connect button to request a lock 
     connect.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, lockUrl, null, 
         new Response.Listener<JSONObject>() { 

          @Override 
          public void onResponse(JSONObject response) { 
           textResponse.setText("Response: " + response.toString()); 
           connect.setText(R.string.Connection_lock); 
           //if the lock is granted then the green button appears 
           mImageView.setImageResource(R.drawable.bullet_green); 
          } 

         }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         connect.setText(R.string.Connection_no); 
         //if no connection then the red button appears 
         mImageView.setImageResource(R.drawable.circle_red); 
         VolleyLog.e("Error: ", error.getMessage()); 

        } 

       }) { 

        @Override 
        public Map<String, String> getParams()throws AuthFailureError { 
         HashMap<String, String> mParams = new HashMap<String,String>(); 
         mParams.put("nameSpace", "accounting"); 
         mParams.put("lockName", "kevin1"); 
         mParams.put("durationSeconds", "600"); 
         mParams.put("userParam", "Cust-20221"); 
         mParams.put("callbackUrl","www.yahoo.com"); 
         return mParams; 
        } 


        @Override 
        public Map<String, String> getHeaders() throws AuthFailureError { 
         HashMap<String, String> headers = new HashMap<String, String>(); 
         headers.put("x-api-key", getResources().getString(R.string.parse_api_key)); 
         return headers; 
        } 

       }; 

       MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest); 
      } 
     }); 
     }} 

싱글 :

public class MySingleton { 

    private static MySingleton mInstance; 
    private static Context mCtx; 
    private RequestQueue requestQueue; 

    private MySingleton(Context context) { 
     mCtx = context; 
     requestQueue = getRequestQueue(); 
    } 

    public RequestQueue getRequestQueue() { 
     if (requestQueue == null) { 
      requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); 
     } 
     return requestQueue; 
    } 

    public static synchronized MySingleton getInstance(Context context) { 
     if (mInstance == null) { 
      mInstance = new MySingleton(context); 
     } 
     return mInstance; 
    } 

    public <T> void addToRequestQueue(Request<T> request) { 
     requestQueue.add(request); 
    } 
} 
+1

오류 메시지가 붙여 주실 래요이 코드를 교체? –

+0

나는 내 전화에서 그것을 실행하고 응답 : { "오류": ", statusCode": ".... 잘못된 입력 매개 변수 ... 누락 된 'lockName'매개 변수 – LeadBox4

+0

나는 그들의 문제가 있다고 생각합니다. lockName 매개 변수를 사용하여 코드를 실행하십시오. 코드가 실행되기 전에 우편 배달부 REST 클라이언트에서 POST 데이터를 보내십시오. –

답변

1

setOnClickListener

String lockUrl = "https://lzx650r9ih.execute-api.us-west-2.amazonaws.com/p01/lock?nameSpace=accounting&lockName=kevin1qq&durationSeconds=600&userParam=Cust-20221&callbackUrl=www.yahoo.com"; 

    StringRequest stringRequest = new StringRequest(Request.Method.GET, lockUrl, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        Log.e("RES : ", "RES IS " + response); 
        textResponse.setText("Response: " + response.toString()); 
        connect.setText(R.string.Connection_lock); 
        //if the lock is granted then the green button appears 
        mImageView.setImageResource(R.drawable.bullet_green); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        connect.setText(R.string.Connection_no); 
        //if no connection then the red button appears 
        mImageView.setImageResource(R.drawable.circle_red); 
        VolleyLog.e("Error: ", error.getMessage()); 
       } 
      }) { 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      headers.put("x-api-key", "sOdgqxUJmm6lKAObnI2Dq5JYv6f4NVot9KMiNMWL"); 
      return headers; 
     } 
    }; 

    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest); 
+0

JSON을 사용해야합니다. 이전 코드 connect.setOnClickListener에서 – LeadBox4

+0

은 (새 View.OnClickListener는() { @Override 공공 무효 온 클릭 (보기 V) { –

+0

그들은 이미 연결되어 있습니다. – LeadBox4