【问题标题】:How to parse json with retrofit on Android如何在 Android 上通过改造解析 json
【发布时间】:2021-04-19 17:46:25
【问题描述】:

我尝试编写解析缩写解码及其使用频率的应用程序。用户输入缩写,例如“kg”。所以,应用应该连接到“http://nactem.ac.uk/software/acromine/dictionary.py?sf=kg”并解析它。

模型类应该是什么样的?我在界面中正确描述了@Geth 吗?如何获取列表,我做了一个适配器,将它绑定到列表,但我不确定getResponse()。

界面:

public interface SService {

@GET("dictionary.py?")
Call<Example> getResponse(@Query("sf=") String searchString);
}

改造客户端:

public class RetrofitClient {

private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl) {
    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}
}

以及添加类ApiUtils

public class ApiUtils {

public static final String BASE_URL = "http://nactem.ac.uk/software/acromine/";

public static SService getSOService() {
    return RetrofitClient.getClient(BASE_URL).create(SService.class);
}
}

主活动

public class MainActivity extends AppCompatActivity {
private ArrayList<Lf> elements = new ArrayList();
EditText editText1;
ElementAdapter stateAdapter;
ListView list;
private SService mService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText1 = (EditText) findViewById(R.id.edit_text);

    mService = ApiUtils.getSOService();

    list = (ListView) findViewById(R.id.fragment_list);
    
    stateAdapter = new ElementAdapter(this, R.layout.list_item, elements);
    
    list.setAdapter(stateAdapter);
}




public void loadAnswers() {
    mService.getResponse(editText1.getText().toString()).enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call, Response<Example> response) {

            if(response.isSuccessful()) {
                stateAdapter.updateAnswers(response.body().getLfs());
            } else {
                int statusCode = response.code();
                // handle request errors depending on status code
            }
        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {

        }
    });
}


public void activity_button(View view) {
    //stateAdapter.clear();
    loadAnswers();
}
}

更新!! 所有输入数据都是一个数组:Array[Object{...}]。解析它的正确接口: 公共接口 SService {

@GET("dictionary.py")
Call<List<Example>> getResponse(@Query("sf") String searchString);
}

所以,我们得到了带有示例对象的调用列表(在这种情况下只有一个对象)。

【问题讨论】:

  • @GET("dictionary.py?") 删除“?”在你的 GET 中。改造会自动添加“?”和“&”当你的参数中有 @Query 时。
  • 同样返回类型必须是数组并在清单中启用清除流量,因为您通过 http 发出请求。

标签: java android retrofit


【解决方案1】:

SService接口改为如下,

public interface SService {

@GET("dictionary.py")
Call<Example> getResponse(@Query("sf") String searchString);
}

【讨论】:

    猜你喜欢
    • 2016-11-28
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 2015-08-18
    • 2016-02-25
    • 2017-07-07
    相关资源
    最近更新 更多