gpt4 book ai didi

Android:在使用 Retrofit 继续执行之前等待 API 的响应

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:41 25 4
gpt4 key购买 nike

我的 android 编程遇到了问题,我有一个应用程序需要运行一些例程,例如扫描一些条形码。一旦我扫描了一个条形码,我就会将这个条形码的数据发送到我的 API,“等待”以获取一些信息,然后再做其他事情。

问题是当我调用我的 API 来获取信息并且在我尝试使用本应来自 API 的信息时,我遇到了 NULL 异常,请参见下面的代码:

我每次扫描条形码时调用的扫描例程:

    // 1 - First Stage: Scan barcode of tank
if(stageScannerTank == 1)
{
// Verify if the user scanned the correct Tank
if (!resultScan.equals(expectedTank)) {
Toast.makeText(getContext(), "Wrong Tank, scan the correct ", Toast.LENGTH_LONG).show();
stageScannerTank = 0;
return;
}


// Get the tank information

//new TanqueTask().execute(resultScan);

**HERE IS MY PROBLEM, When I call the function below, I fill my tankMixPlant with data**

getTankMixPlant(resultScan);

** I always fall into this IF **
// If null information
if(tankMixPlant == null) {
Toast.makeText(getContext(), "Error to read tank information", Toast.LENGTH_LONG).show();
return;
}

}

getTankMixPlant,使用 Retrofit 调用我的服务,如果我测试我的例程,它就可以工作:

private void getTankMixPlant(String tankName){
mService.getTankMixPlant(tankName).enqueue(new Callback<TankMixPlant>() {
@Override
public void onResponse(Call<TankMixPlant> call, Response<TankMixPlant> response) {
tankMixPlant = response.body();

}

@Override
public void onFailure(Call<TankMixPlant> call, Throwable t) {
// On failure show the error message to the User
AlertDialog alert;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Error");
builder.setMessage("Error: " + t.getMessage());
alert = builder.create();
alert.show();
}
});
}

如何等待 tankMixPlant 填满数据才能继续我的日常工作?

更新:

我继续这个问题,但我改变了一些你可以在下面看到的东西:现在我得到 NullPointerException。

现在我在 onResponse 方法中验证我的对象是否为空:

     private void getTankMixPlant(String tankName){
mService.getTankMixPlant(tankName).enqueue(new Callback<TankMixPlant>() {
@Override
public void onResponse(Call<TankMixPlant> call, Response<TankMixPlant> response) {
tankMixPlant = response.body();
if(tankMixPlant == null) {
Toast.makeText(getContext(), "Error to read tank information",
Toast.LENGTH_LONG).show();
return;
}
}

@Override
public void onFailure(Call<TankMixPlant> call, Throwable t) {
// On failure show the error message to the User
AlertDialog alert;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Error");
builder.setMessage("Error: " + t.getMessage());
alert = builder.create();
alert.show();
}
});
}

但是当我尝试验证有关 tankMixPlant 的一些信息时:

        // Verify tank stage
if(!tankMixPlant.getBlenderCode().equals("0") && tankMixPlant.getIngr() == 1)
{
getPackages(tankMixPlant.getRecipeNumber(), tankMixPlant.getBlenderCode(), tankMixPlant.getTankName());
// 2 - Mix Packages: Scan package barcode
stageScannerTank = 2;
}

if(!tankMixPlant.getIngrSAPNumber().equals("0") && tankMixPlant.getIngr() == 1)
{
getIngr(tankMixPlant.getRecipeNumber(), tankMixPlant.getTankName());
// 3 - P.I Packages: Scan package barcode
stageScannerTank = 3;
}

我的应用因以下错误而崩溃:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.labtse.kibonapp, PID: 21519
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=114910, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} (has extras) }} to activity {com.example.labtse.kibonapp/com.example.labtse.kibonapp.TabPagesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.labtse.kibonapp.model.TankMixPlant.getBlenderCode()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:5007)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5050)
at android.app.ActivityThread.access$1600(ActivityThread.java:230)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1876)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.labtse.kibonapp.model.TankMixPlant.getBlenderCode()' on a null object reference
at com.example.labtse.kibonapp.MixPlantFragment.scanRoutine(MixPlantFragment.java:367)
at com.example.labtse.kibonapp.MixPlantFragment.onActivityResult(MixPlantFragment.java:399)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151)
at android.app.Activity.dispatchActivityResult(Activity.java:7165)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5003)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5050) 
at android.app.ActivityThread.access$1600(ActivityThread.java:230) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1876) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7409) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

我的 tankMixPlant 类:

package com.example.labtse.kibonapp.model;

public class TankMixPlant {
private String tankName;
private String recipeNumber;
private int idBatch;
private String ingrSAPNumber;
private String blenderCode;
private short ingr;
private boolean QRUN;
private boolean QEND;
private Boolean QDOSANDO;
private Boolean CONF_ING;

public TankMixPlant() {
}

public String getTankName() {
return tankName;
}

public void setTankName(String tankName) {
this.tankName = tankName;
}

public String getRecipeNumber() {
return recipeNumber;
}

public void setRecipeNumber(String recipeNumber) {
this.recipeNumber = recipeNumber;
}

public int getIdBatch() {
return idBatch;
}

public void setIdBatch(int idBatch) {
this.idBatch = idBatch;
}

public String getIngrSAPNumber() {
return ingrSAPNumber;
}

public void setIngrSAPNumber(String ingrSAPNumber) {
this.ingrSAPNumber = ingrSAPNumber;
}

public String getBlenderCode() {
return blenderCode;
}

public void setBlenderCode(String blenderCode) {
this.blenderCode = blenderCode;
}

public short getIngr() {
return ingr;
}

public void setIngr(short ingr) {
this.ingr = ingr;
}

public boolean isQRUN() {
return QRUN;
}

public void setQRUN(boolean QRUN) {
this.QRUN = QRUN;
}

public boolean isQEND() {
return QEND;
}

public void setQEND(boolean QEND) {
this.QEND = QEND;
}

public Boolean getQDOSANDO() {
return QDOSANDO;
}

public void setQDOSANDO(Boolean QDOSANDO) {
this.QDOSANDO = QDOSANDO;
}

public Boolean getCONF_ING() {
return CONF_ING;
}

public void setCONF_ING(Boolean CONF_ING) {
this.CONF_ING = CONF_ING;
}

最佳答案

解决方法:

你必须这样写:

    if(tankMixPlant == null) {
Toast.makeText(getContext(), "Error to read tank information", Toast.LENGTH_LONG).show();
return;
}

在您的 onResponse 中,tankplant = ... 行之后

现在,如果您从服务器获取任何数据,空异常将被消除。

希望对您有所帮助。

更新1:

这样写:

    if(!tankMixPlant.getBlenderCode().equals("0") && tankMixPlant.getIngr() == 1)
{
getPackages(tankMixPlant.getRecipeNumber(), tankMixPlant.getBlenderCode(), tankMixPlant.getTankName());
// 2 - Mix Packages: Scan package barcode
stageScannerTank = 2;
}

if(!tankMixPlant.getIngrSAPNumber().equals("0") && tankMixPlant.getIngr() == 1)
{
getIngr(tankMixPlant.getRecipeNumber(), tankMixPlant.getTankName());
// 3 - P.I Packages: Scan package barcode
stageScannerTank = 3;
}

在您的 onResponse 方法中,如下所示:

 private void getTankMixPlant(String tankName){
mService.getTankMixPlant(tankName).enqueue(new Callback<TankMixPlant>() {
@Override
public void onResponse(Call<TankMixPlant> call, Response<TankMixPlant> response) {
tankMixPlant = response.body();
if(tankMixPlant == null) {
Toast.makeText(getContext(), "Error to read tank information",
Toast.LENGTH_LONG).show();
return;
}

...... (Write Here)

}

@Override
public void onFailure(Call<TankMixPlant> call, Throwable t) {
// On failure show the error message to the User
AlertDialog alert;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Error");
builder.setMessage("Error: " + t.getMessage());
alert = builder.create();
alert.show();
}
});

试一试。

关于Android:在使用 Retrofit 继续执行之前等待 API 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52593230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com