使用 Android Kotlin 发送 Post 请求
Send Post request using Android Kotlin
我正在尝试在 Android 中使用 Kotlin 发送 POST 请求。我需要使用 Volley,并且需要使用不记名令牌进行身份验证。我不知道如何将不记名令牌与 post 请求一起发送。
val queue = Volley.newRequestQueue(this)
val parameters: MutableMap<String, String> = HashMap()
parameters.put("token_name", "app");
val strReq: StringRequest = object : StringRequest(
Method.POST, "https://url.com",
Response.Listener { response ->
try {
val responseObj = JSONObject(response)
val id = responseObj.getInt("id")
val token = responseObj.getString("name")
val tipo = responseObj.getString("email")
} catch (e: Exception) { // caught while parsing the response
}
},
Response.ErrorListener { volleyError ->
})
您可以覆盖 volley 的请求方法:
public void requestWithSomeHttpHeaders(Context context) {
RequestQueue queue = Volley.newRequestQueue(context);
String url = "http://www.somewebsite.com";
StringRequest getRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// response
try {
JSONObject responseObj = new JSONObject(response);
int id = responseObj.getInt("id");
String token = responseObj.getString("name");
String tipo = responseObj.getString("email");
} catch (Exception e) { // caught while parsing the response
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.d("ERROR", "error => " + error.toString());
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
//insert your bearer token here
params.put("bearer token", "xxxxxx");
return params;
}
};
queue.add(getRequest);
}
当然,你可以阅读这个post作为参考。
我正在尝试在 Android 中使用 Kotlin 发送 POST 请求。我需要使用 Volley,并且需要使用不记名令牌进行身份验证。我不知道如何将不记名令牌与 post 请求一起发送。
val queue = Volley.newRequestQueue(this)
val parameters: MutableMap<String, String> = HashMap()
parameters.put("token_name", "app");
val strReq: StringRequest = object : StringRequest(
Method.POST, "https://url.com",
Response.Listener { response ->
try {
val responseObj = JSONObject(response)
val id = responseObj.getInt("id")
val token = responseObj.getString("name")
val tipo = responseObj.getString("email")
} catch (e: Exception) { // caught while parsing the response
}
},
Response.ErrorListener { volleyError ->
})
您可以覆盖 volley 的请求方法:
public void requestWithSomeHttpHeaders(Context context) {
RequestQueue queue = Volley.newRequestQueue(context);
String url = "http://www.somewebsite.com";
StringRequest getRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// response
try {
JSONObject responseObj = new JSONObject(response);
int id = responseObj.getInt("id");
String token = responseObj.getString("name");
String tipo = responseObj.getString("email");
} catch (Exception e) { // caught while parsing the response
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.d("ERROR", "error => " + error.toString());
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
//insert your bearer token here
params.put("bearer token", "xxxxxx");
return params;
}
};
queue.add(getRequest);
}
当然,你可以阅读这个post作为参考。