我正在做一个简单的应用程序,此时您需要做的就是按连接按钮以请求授予锁定请求。如果已授予,则会显示JSON响应。这需要一个HTTP标头(X-API-key),我将其粘贴到strings.xml中。我在标题中指定了它。我也包括了参数。我已经在postman中测试了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);
}
});
}}
的Singleton:
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);
}
}
答案 0 :(得分: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);