调用印度铁路PNR查询API后返回Null的问题排查求助
Android调用印度铁路PNR状态查询API返回Null排查
我在Android项目中调用RapidAPI提供的印度铁路PNR状态查询API后,测试用的TextView输出了Null,想确认API是否正常工作,附上相关代码请求排查原因。
MainActivity代码
package com.example.trainman import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.widget.TextView import retrofit2.Call import retrofit2.Callback import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val retrofitBuilder = Retrofit.Builder() .baseUrl("https://pnr-status-indian-railway.p.rapidapi.com") .addConverterFactory(GsonConverterFactory.create()) .build() .create(ApiInterface::class.java) val retrofitData = retrofitBuilder.getData(query = "6902637622") retrofitData.enqueue(object : Callback<myData?>{ override fun onResponse(call: Call<myData?>, response: Response<myData?>) { //if the api call is success val dataList=response.body()?.data val textView=findViewById<TextView>(R.id.tv2) textView.text=dataList.toString() Log.d("TAG: onResponse","onResponse: "+ response.body()) } override fun onFailure(call: Call<myData?>, t: Throwable) { //if the api call fail Log.d("TAG: onFailure","onFailure"+ t.message) } }) } }
activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg1" android:orientation="vertical" tools:context=".MainActivity"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/txtInput" android:layout_width="match_parent" android:layout_height="59sp" android:layout_margin="25sp" android:textAlignment="center" app:counterEnabled="true" app:startIconDrawable="@drawable/search"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/rectangle" android:hint="@string/enter_your_pnr_no" android:inputType="number" android:maxLength="10" android:paddingStart="55sp" android:paddingTop="5sp" android:paddingEnd="0sp" android:paddingBottom="5sp" android:textSize="27sp" android:textStyle="bold" /> </com.google.android.material.textfield.TextInputLayout> <TextView android:id="@+id/tv2" android:layout_width="match_parent" android:layout_height="281dp" android:background="#222" android:textColor="#F4F0F0" android:gravity="center" android:text="@string/hello_txt" android:textSize="27sp"> </TextView> <Button android:id="@+id/btnSearch" android:layout_width="196dp" android:layout_height="62dp" android:layout_gravity="center" android:layout_margin="350dp" android:background="@drawable/buttonbg" android:padding="10sp" android:text="@string/pnr_search" android:textSize="25sp" /> </LinearLayout>
ApiInterface代码(已移除API密钥)
package com.example.trainman import retrofit2.Call import retrofit2.http.GET import retrofit2.http.Headers import retrofit2.http.Query interface ApiInterface { @Headers("X-RapidAPI-Key:47f*************************************0", "X-RapidAPI-Host:pnr-status-indian-railway.p.rapidapi.com") @GET("PNRnumber") fun getData(@Query("PNR") query:String) : Call<myData> }
排查建议
- 验证API可用性:用Postman或curl直接调用该API,传入PNR号
6902637622并带上正确的请求头,确认返回结果是否正常。如果直接调用也无数据,可能是PNR号无效或API本身问题。 - 检查数据模型匹配度:确认
myData类的结构和API返回的JSON完全对应,特别是data字段的名称和类型。可以在onResponse中添加Log.d("RAW_RESPONSE", response.raw().body()?.string() ?: "Empty")查看原始返回内容,对比模型类字段。 - 确认网络权限:检查
AndroidManifest.xml中是否添加了<uses-permission android:name="android.permission.INTERNET" />,无此权限会导致网络请求失败。 - 分析响应状态:在
onResponse中打印response.code(),如果不是200,调用response.errorBody()?.string()查看API返回的错误信息;如果是200但response.body()为Null,说明Gson解析失败,需检查模型类。 - 核对API端点与参数:确认
@GET("PNRnumber")是正确的API端点,@Query("PNR")的参数名是否和API文档要求一致(部分API可能要求小写pnr)。
内容的提问来源于stack exchange,提问作者Amank
相关产品推荐
相关产品推荐

