2017-02-19 98 views
-1

我用retrofix请求数据,但结果显示错键安卓:POST请求与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); 

}

我的MainActivity代码:

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); 
    } 
} 
+0

你的问题是什么?我可以点击上面的链接,我想我是** GET **方法,而不是** POST **方法。你可以发布你的'结果'模型 –

+0

是的,我可以使用get和post,也可以使用但我想用post来试一下。如果您想使用** POST **与** @ body **,则您的服务器必须支持该模式,所以我发布了结果模型 – Alice

+0

。现在我看到你的链接,它支持'GET'和'@ Query'参数 –

回答

0

会推荐改变你的重新trofit接口以下。

public interface Api { 
@POST("bus/station") 
Call<Result> check(@Query("dtype") String dtype, @Query("city") String city, @Query("station") String station, @Query("key") String key); 
} 
+0

如果我想用@ body来做它,那该怎么做 – Alice

+0

为什么你要用@Body? –

+0

我只是学习改造,所以想尝试 – Alice