我想从网址
获取数据 http://cloud.traffy.in.th/apis/apitraffy.php?api=get_bus_line_info&appid=51248f9a&bus_line={bus_line}
和{bus_line}
示例
http://cloud.traffy.in.th/apis/apitraffy.php?api=get_bus_line_info&appid=51248f9a&bus_line=17
但我尝试通过retrofit2获取数据,现在我收到错误
D / Resultttt:Not Found
Bus.java
公共班巴士{
@SerializedName("bus_line")
@Expose
private String busLine;
@SerializedName("start")
@Expose
private String start;
@SerializedName("end")
@Expose
private String end;
@SerializedName("vehicle_type")
@Expose
private String vehicleType;
@SerializedName("owner")
@Expose
private String owner;
@SerializedName("route_type")
@Expose
private String routeType;
@SerializedName("bus_line_id")
@Expose
private String busLineId;
public String getBusLine() {
return busLine;
}
public void setBusLine(String busLine) {
this.busLine = busLine;
}
....
}
Getbus.java
public interface Getbus {
@GET("apis/apitraffy.php")
Call<Bus> select(@Query("api") String api, @Query("appid") String appId, @Query("bus_line") String bus_line);
}
和MainActivity.java
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
TextView textView;
private Getbus getbus;
private String BASE_URL = "http://cloud.traffy.in.th/";
private String TAG = "fong";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
getbus = retrofit.create(Getbus.class);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String store = editText.getText().toString(); //////store=17
loadBus(store);
}
});
}
private void loadBus(String bus_line){
Call<Bus> call = getbus.select(api, appId, bus_line);
call.enqueue(new Callback<Bus>() {
@Override
public void onResponse(Call<Bus> call, Response<Bus> response) {
// displayBus(response.body());
if (response.isSuccessful()){
Log.d("Result", response.body().getBusLine());
}else
{
Log.d("Resultttt",response.message());
}
}
@Override
public void onFailure(Call<Bus> call, Throwable t) {
Log.e(TAG,"Errorr: " + t.getMessage());
}
});
}
private void displayBus(Bus showBus){
if(showBus != null){
String data = showBus.getBusLine();
textView.setText(data);
}else {
textView.setText("No Busline");
}
}
}
我想从edittext获取文本,并将{bus_line}
替换为show data
感谢您的帮助!
答案 0 :(得分:0)
错误消息告诉您确切的错误。你的界面应该更像这样:
public interface Getbus {
@GET("apis/apitraffy.php")
Call<Bus> select(@Query("api") String api, @Query("appid") String appId, @Query("bus_line") String bus_line);
}
如果您不想每次都传递api和app id,请查看请求拦截器。
对于日志记录,请将其添加到build.gradle依赖项:compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
以及构建改造的地方:
final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.addInterceptor(loggingInterceptor);
final OkHttpClient client = clientBuilder.build();
Retrofit retrofit = retrofitBuilder
.baseUrl(params.url)
.client(client)
.build();