You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Retrofit使用疑问:如何通过QueryMap调用带参数的API接口

Retrofit处理Base URL、QueryMap参数及RecyclerView展示指南

我来一步步帮你解决Retrofit调用API的问题,从接口定义、参数传递到最终的RecyclerView展示,全程给你具体的代码示例:


1. 修正Base URL配置

首先你当前的Base URL少了.com,应该修正为:

// 注意结尾加斜杠,或者接口路径以/开头,Retrofit会自动处理拼接
String BASE_URL = "https://api.domainname.com/25/SmartService.svc/";

2. 定义Retrofit接口(Interface.java)

用@GET指定接口剩余路径,用@QueryMap批量传递查询参数:

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;
import java.util.Map;

public interface ApiInterface {
    // 对应API中Base URL之后的路径部分:GetFundslistByPANIMEI
    @GET("GetFundslistByPANIMEI")
    Call<Example> getFundsList(@QueryMap Map<String, String> queryParams);
}

3. 构建参数Map并发起请求

在MainActivity中初始化Retrofit,组装参数,调用接口:

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.util.HashMap;
import java.util.Map;

// 初始化Retrofit
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

ApiInterface apiService = retrofit.create(ApiInterface.class);

// 组装查询参数(注意参数名要和API要求完全一致)
Map<String, String> params = new HashMap<>();
params.put("IMEI", "ODY2NzQxMDMzNDQwMjQx%0A");
params.put("Fund", "");
params.put("APKVer", "NC4zMg%3D%3D%0A");
params.put("pan", "QUZZUE01NzE5Qg%3D%3D%0A");
params.put("Adminusername", "c21hcnRzZXJ2aWNl");
params.put("OS", "QW5kcm9pZA%3D%3D%0A");
params.put("Adminpassword", "a2FydnkxMjM0JTI0");

// 发起异步请求
Call<Example> call = apiService.getFundsList(params);
call.enqueue(new Callback<Example>() {
    @Override
    public void onResponse(Call<Example> call, Response<Example> response) {
        if (response.isSuccessful() && response.body() != null) {
            Example apiResponse = response.body();
            // 获取需要展示的基金列表(来自Table1)
            List<Table1> fundList = apiResponse.getTable1();
            // 把数据传给RecyclerView展示
            setupRecyclerView(fundList);
        } else {
            // 处理请求失败的情况
            Toast.makeText(MainActivity.this, "请求失败,请检查参数", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(Call<Example> call, Throwable t) {
        // 处理网络错误
        Toast.makeText(MainActivity.this, "网络异常:" + t.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

4. 实现RecyclerView展示

4.1 创建列表项布局(res/layout/item_fund.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp"
    android:background="?attr/selectableItemBackground">

    <TextView
        android:id="@+id/tv_fund_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/tv_fund_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_marginTop="8dp"/>

</LinearLayout>

4.2 编写RecyclerView适配器(FundAdapter.java)

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class FundAdapter extends RecyclerView.Adapter<FundAdapter.FundViewHolder> {
    private List<Table1> fundList;

    public FundAdapter(List<Table1> fundList) {
        this.fundList = fundList;
    }

    @NonNull
    @Override
    public FundViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_fund, parent, false);
        return new FundViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FundViewHolder holder, int position) {
        Table1 fund = fundList.get(position);
        holder.tvFundId.setText("基金ID:" + fund.getFundId());
        holder.tvFundDesc.setText("基金名称:" + fund.getFNFundDescription());
    }

    @Override
    public int getItemCount() {
        return fundList.size();
    }

    static class FundViewHolder extends RecyclerView.ViewHolder {
        TextView tvFundId, tvFundDesc;

        public FundViewHolder(@NonNull View itemView) {
            super(itemView);
            tvFundId = itemView.findViewById(R.id.tv_fund_id);
            tvFundDesc = itemView.findViewById(R.id.tv_fund_desc);
        }
    }
}

4.3 初始化RecyclerView

在MainActivity中添加setupRecyclerView方法:

private void setupRecyclerView(List<Table1> fundList) {
    RecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    FundAdapter adapter = new FundAdapter(fundList);
    recyclerView.setAdapter(adapter);
}

5. 必要配置

  • 添加依赖:在app/build.gradle中加入以下依赖:
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'androidx.recyclerview:recyclerview:1.3.2'
    
  • 网络权限:在AndroidManifest.xml中添加:
    <uses-permission android:name="android.permission.INTERNET"/>
    

内容的提问来源于stack exchange,提问作者Teja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 15:02:40