[英]Android Volley Json Request: Parameters not showing
I'm doing a simple app where all you have to do at this point is press the connect button to ask for a lock request to be granted. 我正在做一个简单的应用程序,此时您所要做的就是按连接按钮以请求授予锁定请求。 If it is granted, then the JSON response appears.
如果被授予,则显示JSON响应。 This requires an HTTP header( X-API-key) which I pasted into strings.xml.
这需要一个HTTP标头(X-API-key),我将其粘贴到strings.xml中。 I specified it in the headers.
我在标题中指定了它。 I included the parameters as well.
我也包括了参数。 I already tested the URL in postman and it's working fine.
我已经在邮递员中测试了URL,并且工作正常。 Here, an error message appears.
在这里,出现错误信息。 As you'll see below, I have two classes: Main Activity and MySingleton.
正如您将在下面看到的,我有两个类:Main Activity和MySingleton。 Thank you in advance.
先感谢您。
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);
}
}
Replace this code in setOnClickListener 在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.