Android:使用Retrofix发布请求

时间:2017-02-19 03:21:58

标签: android retrofit

我使用retrofix请求数据,但结果显示错误的密钥

发布网址:http://op.juhe.cn/189/bus/station?dtype=json&city=佛山&station=祖庙&key=b7f4857671be4512300c0f52774e5a3c

我的HTTP API进入Java界面:

public interface Api {
@POST("bus/station")
Call<Result> check(@Body UrlParam param);

}

我的主要活动代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.bnt01)
Button btn01;
@BindView(R.id.tv01)
TextView tv01;
private Api api;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    btn01.setOnClickListener(this);
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://op.juhe.cn/189/").addConverterFactory(GsonConverterFactory.create()).build();
    api = retrofit.create(Api.class);

}

private UrlParam getUrlParam() {
    UrlParam param = new UrlParam("json", "佛山", "祖庙", "b7f4857671be4512300c0f52774e5a3c");
    return param;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bnt01:
            UrlParam p = getUrlParam();
            Call<Result> re = api.check(getUrlParam());
            Log.i("TAG", re.toString() + "//" + api.toString());
            re.enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    tv01.setText(response.body().getReason());
                }

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

                }
            });
            break;
    }
  }
}

这是结果模型代码:

  public class Result {
  private String reason;
  private DetailParam result;
  private String errorCode;

public Result(String errorCode, DetailParam result, String reason) {
    this.errorCode = errorCode;
    this.result = result;
    this.reason = reason;
}

public String getReason() {
    return reason;
}

public void setReason(String reason) {
    this.reason = reason;
}

public DetailParam getDetail() {
    return result;
}

public void setDetail(DetailParam result) {
    this.result = result;
}

public String getErrorCode() {
    return errorCode;
}

public void setErrorCode(String errorCode) {
    this.errorCode = errorCode;
}

@Override
public String toString() {
    return new Gson().toJson(this);
   }
}

1 个答案:

答案 0 :(得分:0)

建议您将改装界面更改为以下内容。

display: block