[英]Android Volley Json Request: Parameters not showing
我正在做一個簡單的應用程序,此時您所要做的就是按連接按鈕以請求授予鎖定請求。 如果被授予,則顯示JSON響應。 這需要一個HTTP標頭(X-API-key),我將其粘貼到strings.xml中。 我在標題中指定了它。 我也包括了參數。 我已經在郵遞員中測試了URL,並且工作正常。 在這里,出現錯誤信息。 正如您將在下面看到的,我有兩個類:Main Activity和MySingleton。 先感謝您。
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);
}
}
在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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.