Android 改造 GET

Android Retrofit GET

我是 Retrofit 的新手,并使用它构建了一个简单的应用程序。但是当我尝试 运行 应用程序时,它没有显示从网络上获取的数据。但是,api 的点击率每次都在增加。请帮我解决这个问题。 提前致谢。

主要Activity:

package com.vyshnav.trainname;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.vyshnav.trainname.model.Days;
import com.vyshnav.trainname.model.Train;
import com.vyshnav.trainname.model.TrainNameResponse;
import com.vyshnav.trainname.rest.ApiClient;
import com.vyshnav.trainname.rest.ApiInterface;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

// API KEY: iqccq8931


// TODO - insert your themoviedb.org API KEY here
private final static String API_KEY = "iqccq8931";
private final static String TRAIN_NAME = "BHOPAL EXPRESS";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if (API_KEY.isEmpty()) {
        Toast.makeText(getApplicationContext(), "Please obtain your API KEY first from themoviedb.org", Toast.LENGTH_LONG).show();
        return;
    }

    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
    Call<TrainNameResponse> call = apiService.getTrainNameResponse(TRAIN_NAME,API_KEY);
    call.enqueue(new Callback<TrainNameResponse>() {
        @Override
        public void onResponse(Call<TrainNameResponse>call, Response<TrainNameResponse> response) {
          //  Class<Train> trainObj = response.body().getTrain();

            String tv1 = response.body().getTrain().getName();
            TextView textView = (TextView) findViewById(R.id.tv1);
            textView.setText(tv1);

            int tv2 = response.body().getResponse_code();
            TextView textView2 = (TextView) findViewById(R.id.tv2);
            textView2.setText(tv2);
        }

        @Override
        public void onFailure(Call<TrainNameResponse>call, Throwable t) {
            // Log error here since request failed
        }
    });
}

}

ApiClient.java

package com.vyshnav.trainname.rest;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class ApiClient {

public static final String BASE_URL = "http://api.railwayapi.com/name_number/";
private static Retrofit retrofit = null;


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

ApiInterface.java

package com.vyshnav.trainname.rest;

import com.vyshnav.trainname.model.TrainNameResponse;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * Created by Vyshnav on 15-08-2016.
 */

public interface ApiInterface {

@GET("train/{name}/apikey/{apikey}/")
Call<TrainNameResponse> getTrainNameResponse(@Path("name") String name, @Path("apikey") String apiKey);
}

TrainNameResponse.java

package com.vyshnav.trainname.model;

import com.google.gson.annotations.SerializedName;


public class TrainNameResponse {

@SerializedName("train")
Class<Train> train;
@SerializedName("response_code")
int response_code;

public TrainNameResponse(Class<Train> train,int response_code) {
    this.train = train;
    this.response_code = response_code;
}

public Class<Train> getTrain() {

    return train;
}

public void setTrain(Class<Train> train) {

    this.train = train;
}

public int getResponse_code() {

    return response_code;
}

public void setResponse_code(int response_code) {

    this.response_code = response_code;
}
}

Train.java

package com.vyshnav.trainname.model;

import com.google.gson.annotations.SerializedName;

import java.util.List;


public class Train {

@SerializedName("days")
private List<Days> days;
@SerializedName("name")
private String name;
@SerializedName("number")
private String number;

public Train(List<Days> days,String name, String number) {
    this.days = days;
    this.name = name;
    this. number = number;
}

public List<Days> getDays() {

    return days;
}

public void setDays(List<Days> days) {

    this.days = days;
}

public String getName() {

    return name;
}

public void setName(String name) {

    this.name = name;
}

public String getNumber() {

    return number;
}

public void setNumber(String number) {

    this.number = number;
}


}

Days.java

package com.vyshnav.trainname.model;

import com.google.gson.annotations.SerializedName;

public class Days {

@SerializedName("runs")
private String runs;
@SerializedName("day-code")
private String daycode;

public Days (String runs,String daycode) {
    this.runs = runs;
    this.daycode = daycode;
}

public String getRuns() {
    return runs;
}

public void setRuns(String runs) {
    this.runs = runs;
}

public String getDaycode() {
    return daycode;
}

public void setDaycode(String daycode) {
    this.daycode = daycode;
}
}

为什么只有一些 getter 和 setter 是橙色的,而另一些不是?

虽然应用程序没有崩溃,但我还在 logcat 中发现了 2 个问题。 Logcat:

08-16 16:23:25.032 13558-13558/com.vyshnav.trainname D/AccessibilityManager: setStateLocked: wasEnabled = false, mIsEnabled = false, wasTouchExplorationEnabled = false, mIsTouchExplorationEnabled = false, wasHighTextContrastEnabled = false, mIsHighTextContrastEnabled = false
                                                                         java.lang.Throwable: setStateLocked
                                                                             at android.view.accessibility.AccessibilityManager.setStateLocked(AccessibilityManager.java:553)
                                                                             at android.view.accessibility.AccessibilityManager.tryConnectToServiceLocked(AccessibilityManager.java:636)
                                                                             at android.view.accessibility.AccessibilityManager.<init>(AccessibilityManager.java:226)
                                                                             at android.view.accessibility.AccessibilityManager.getInstance(AccessibilityManager.java:206)
                                                                             at android.view.View.setFlags(View.java:9941)
                                                                             at android.view.ViewGroup.initViewGroup(ViewGroup.java:536)
                                                                             at android.view.ViewGroup.<init>(ViewGroup.java:525)
                                                                             at android.view.ViewGroup.<init>(ViewGroup.java:520)
                                                                             at android.view.ViewGroup.<init>(ViewGroup.java:516)
                                                                             at android.view.ViewGroup.<init>(ViewGroup.java:512)
                                                                             at android.widget.FrameLayout.<init>(FrameLayout.java:119)
                                                                             at com.android.internal.policy.impl.PhoneWindow$DecorView.<init>(PhoneWindow.java:2346)
                                                                             at com.android.internal.policy.impl.PhoneWindow.generateDecor(PhoneWindow.java:3643)
                                                                             at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:4036)
                                                                             at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:2057)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:363)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
                                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                             at com.vyshnav.trainname.MainActivity.onCreate(**MainActivity.java:34**)
                                                                             at android.app.Activity.performCreate(Activity.java:6142)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
                                                                             at android.app.ActivityThread.access0(ActivityThread.java:178)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                             at android.os.Looper.loop(Looper.java:194)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5691)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

08-16 16:23:25.962 13558-13603/com.vyshnav.trainname E/GED: Failed to get GED Log Buf, err(0)

请在 onFailure 中打印 Exception 这将帮助您找到问题