我想在Android中编辑url以获取GET响应

问题描述 投票:0回答:1

url:https://ashish.gupta.in/ins.svc/test/ {abc} / {xyz}

编辑后,我想要这个:https://ashish.gupta.in/ins.svc/test/1234/lmnop

android android-studio url android-volley
1个回答
0
投票

您可以像这样简单地创建请求

科特琳:

fun fetchDataFromAPI(varargs params:String){
       val jsonObjectRequest = JsonObjectRequest(Request.Method.GET, 
        "https://ashish.gupta.in/ins.svc/test/${params[0]}/${params[1]}", null,
        Response.Listener { response ->
            Log.d("Response",response.toString())
        },
        Response.ErrorListener { error ->
            // TODO: Handle error
        }
      )
    requestQueue.add(jsonObjectRequest )  // use your requestqueue ibject to send the request
 }

Java:

void fetchDataFromAPI(String.. params){
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
        (Request.Method.GET, "https://ashish.gupta.in/ins.svc/test/"+params[0]+"/"+params[1]}, null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
         Log.d("Response",response.toString());
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO: Handle error

    }
});
requestQueue.add(jsonObjectRequest); // use your requestqueue ibject to send the request

}

现在您调用类似的函数:

fetchDataFromAPI("1234","lmnop")
© www.soinside.com 2019 - 2024. All rights reserved.