提问人:Sarmad Ali 提问时间:10/2/2023 最后编辑:Sarmad Ali 更新时间:10/2/2023 访问量:50
使用 localhost 在 android studio 中实现 API 时出现的问题
Problem in Implementing the API in android studio using localhost
问:
我正在尝试实现一个简单的 API 来从 localhost 读取数据。API 在 POSTMAN 中工作正常。但是,当我使用 RETROFIT 实现它时,它不会从 localhost 加载数据。我已确保检查网络是否接受 80 的响应,这很好。而且我还收到了 recyclerview 的另一个错误。
以下是这 2 个错误:
1: RecyclerView com.sarmadali.poetryapp E 未连接适配器;跳过布局
另一个是:
java.lang.NullPointerException:尝试在 com.sarmadali.poetryapp.Adapters.PoetryAdapters.getItemCount(PoetryAdapters.java:50) 处的空对象引用上调用虚拟方法“int java.util.ArrayList.size()”
这是我的 MainActivity 代码:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerViewPoetry;
PoetryAdapters poetryAdapters;
ApiInterfacePoetry apiInterfacePoetry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitializationMethod();
apiInterfacePoetry.getPoetry().enqueue(new Callback<GetPoetryResponse>() {
@Override
public void onResponse(Call<GetPoetryResponse> call, Response<GetPoetryResponse> response) {
try {
if (response!=null){
if (response.body().getStatus().equals("1")){
SetAdapter(response.body().getPoetryData());
}else {
Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){
Log.e("Failure", e.getLocalizedMessage());
}
}
@Override
public void onFailure(Call<GetPoetryResponse> call, Throwable t) {
Log.e("Failure Failure", t.getLocalizedMessage());
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
}
private void InitializationMethod(){
recyclerViewPoetry = findViewById(R.id.recyclerViewPoetry);
//initializing apis
Retrofit retrofit = ApiClient.getClient();
apiInterfacePoetry = retrofit.create(ApiInterfacePoetry.class);
// poetryModelArrayList = new ArrayList<>();
}
private void SetAdapter(ArrayList<PoetryModel> poetryLists){
poetryAdapters = new PoetryAdapters(this, poetryLists);
//set layout manager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerViewPoetry.setLayoutManager(linearLayoutManager);
recyclerViewPoetry.setAdapter(poetryAdapters);
}
}
ApiClient.java
public class ApiClient {
public static Retrofit retrofit=null;
public static Retrofit getClient(){
if (retrofit==null) {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Gson gson = new GsonBuilder().create();
retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.10.7/PHPPoetryApis/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
诗歌适配器.java
public class PoetryAdapters extends RecyclerView.Adapter<PoetryAdapters.ViewHolderClass>{
Context context;
ArrayList<PoetryModel> poetryList;
//constructor
public PoetryAdapters(Context context, ArrayList<PoetryModel> poetryModels) {
this.context = context;
this.poetryList = poetryModels;
}
@NonNull
@Override
public ViewHolderClass onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//inflating the layout
View view = LayoutInflater.from(context).inflate(R.layout.poetry_list_view_design, null);
return new ViewHolderClass(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolderClass holder, int position) {
//set all the data
holder.poetName.setText(poetryList.get(position).getP_Name());
holder.p_Poetry.setText(poetryList.get(position).getPoetry());
holder.dateTime.setText(poetryList.get(position).getDate_Time());
}
@Override
public int getItemCount() {
return poetryList.size();
}
//viewholder class created
public class ViewHolderClass extends RecyclerView.ViewHolder{
TextView poetName, p_Poetry, dateTime;
Button btnUpdatePoetry, btnDeletePoetry;
public ViewHolderClass(@NonNull View itemView) {
super(itemView);
poetName = itemView.findViewById(R.id.textPoetName);
p_Poetry = itemView.findViewById(R.id.textPoetry);
dateTime = itemView.findViewById(R.id.textDatenTime);
btnUpdatePoetry = itemView.findViewById(R.id.buttonUpdate);
btnDeletePoetry = itemView.findViewById(R.id.buttonDelete);
}
}
}
更新
排队方法的更改:
apiInterfacePoetry.getPoetry().enqueue(new Callback<GetPoetryResponse>() {
@Override
public void onResponse(Call<GetPoetryResponse> call, Response<GetPoetryResponse> response) {
try {
if (response.isSuccessful() && response.body() != null) {
if (response.body().getStatus().equals("1")) {
Log.e("Status Poetry", response.body().getStatus());
ArrayList<PoetryModel> poetryData = response.body().getPoetryData();
if (poetryData != null) {
SetAdapter(poetryData);
} else {
Toast.makeText(MainActivity.this, "Poetry data is null", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "API call was not successful", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Log.e("Failure", e.getLocalizedMessage());
}
}
@Override
public void onFailure(Call<GetPoetryResponse> call, Throwable t) {
Log.e("Failure Failure", t.getLocalizedMessage());
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
在这里,第一次检查成功,并且 Toast 响应是 API 中设置的“可用记录”
if (response.isSuccessful() && response.body() != null)
第二个是
if (response.body().getStatus().equals(“1”)) 它还在日志中返回 1,表示记录可用。
还有这个
poetryData != 空
为 null,因为这会返回 null。
response.body().getPoetryData()
我认为这里没有任何与网络相关的问题。
我希望这有助于回答我的问题。
答:
我通过将 API 响应 JSON 的结构与我的 GetPoetryResponse 类的结构进行比较来实现这一点。类结构与 JSON 结构(包括字段名称和层次结构)不匹配。这些不匹配导致 Gson 无法正确反序列化 JSON,因此我从 GetPoetryResponse() 收到 null 异常。现在一切都按顺序进行。
评论
ApiClient
PoetryAdapters
response.body().getPoetryData()
null