【问题标题】:App crashes without displaying error (Retrofit 2)应用程序崩溃但不显示错误(改造 2)
【发布时间】:2017-05-08 07:30:13
【问题描述】:

我正在学习改造,我只是想将 json 转换为 gson。可能是因为我不熟悉链接的json格式的结构。我尝试了另一个格式更简单的链接,并且该应用程序有效。我想查询“数据”中的内容。我希望你能给我一些建议并找出问题所在。顺便说一句,我为这两个链接制作了单独的应用程序。但过程类似。

这是我应该使用的链接:

Link that makes app crash

这里是教程给出的链接

working link

这是我的界面:

public interface HTTPService {

@GET("/organizations?type=json")
Call<Organization> getMyJSON();

}

这是我的客户:

public class HTTPClient {
private static final String ROOT_URL = "http://isteward.tastradedev.com/api";

/**
 * Get Retrofit Instance
 */
private static Retrofit getRetrofitInstance() {
    return new Retrofit.Builder()
            .baseUrl(ROOT_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}

/**
 * Get API Service
 *
 * @return API Service
 */
public static HTTPService getApiService() {
    return getRetrofitInstance().create(HTTPService.class);
}

}

这是我的 MainActivity:

public class MainActivity extends AppCompatActivity {
private ListView listView;
private View parentView;

private ArrayList<Datum> datalist;
private ListAdapter adapter;
ProgressDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    datalist = new ArrayList<>();
    parentView = findViewById(R.id.parentlayout);


    listView = (ListView) findViewById(R.id.listView);


    dialog = new ProgressDialog(MainActivity.this);
    dialog.setTitle("Fetching Data");
    dialog.setMessage("Please wait...");
    dialog.show();

    //Creating an object of our api interface
    HTTPService api = HTTPClient.getApiService();

    Call<Organization> call = api.getMyJSON();

    call.enqueue(new Callback<Organization>() {
        @Override
        public void onResponse(Call<Organization> call, Response<Organization> response) {
            //Dismiss Dialog
            dialog.dismiss();

            if (response.isSuccessful()) {

                datalist = response.body().getData();


                adapter = new ListAdapter(MainActivity.this, datalist);
                listView.setAdapter(adapter);

            } else {
                Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<Organization> call, Throwable t) {

        }
    });
}

}

我已经使用 jsonschema2pojo 生成了这些类。 该链接由 3 个类组成。我想这会使我的帖子太长。感谢您的帮助。

编辑:我想我必须包括我的适配器

这是我的适配器的一些部分:

public class ListAdapter extends ArrayAdapter<Datum> {
ArrayList<Datum> datalist;
Context context;

private LayoutInflater inflater;


public ListAdapter(Context context, ArrayList<Datum> objects) {
    super(context, 0, objects);
    this.context = context;
    this.inflater = LayoutInflater.from(context);
    datalist = objects;
}

@Override
public Datum getItem(int position) {
    return datalist.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
        View view = inflater.inflate(R.layout.row, parent, false);
        vh = ViewHolder.create((RelativeLayout) view);
        view.setTag(vh);
    } else {
        vh = (ViewHolder) convertView.getTag();
    }

【问题讨论】:

  • 尝试使用 okhttp HttpLoggingInterceptor 调试来自服务器的响应。
  • Android Monitor 中必须有 log cat 条目。请把它贴在这里,我们需要检查它。
  • here Call 您正在从服务器获取组织,但 api 显示它是子级。如果您从 jsonschema2pojo 生成类,则使用 E​​xample class 。像调用
  • @DivyeshPatel 感谢先生的解释。我现在明白了。但是,我怎样才能获取“基准”类中的数据,因为“示例”类中没有进程,与具有“getData()”的“组织”类不同?
  • 您的 json 是嵌套的,因此使用 Call 使用整个 json 作为响应,然后在 datalist = response.body().getData();使用 datalist = response.body().getOrganisation.getData();

标签: android json retrofit retrofit2 gson


【解决方案1】:

首先,当您使用改造时,请按照以下方式进行操作

http://isteward.tastradedev.com

请只提供改造后的域应该进入这样的界面

@GET("api/organizations?type=json")

请不要在根网址后添加任何斜杠

http://isteward.tastradedev.com

@GET("/api/organizations?type=json")

而且我在您的回复中看到了一些空字符串。请将它们转换为 null 或传递其他内容(这可能是您的问题)。

【讨论】:

【解决方案2】:

我的声誉不允许我评论您的问题,但您不能将 json 转换为字符串,然后将字符串转换为 gson 吗?

【讨论】:

  • 对不起先生,但我的任务是做“自动 gson”
【解决方案3】:

为了获取特定的数组/字符串,您必须使用父对象来获取数据。您不能直接使用该 POJO 获取嵌套数组。所以改变api类如下:

首先制作从服务器接受整个 JSON 的 api 接口:

@GET("/organizations?type=json")
Call<Example> getMyJSON();

这里,示例类包含 JSON 响应的 getter/setter。

现在,当您请求 API 时,使用该类回调成功获得响应,

Call<Example> call = api.getMyJSON();

    call.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call, Response<Example> response) {
            //Dismiss Dialog
            dialog.dismiss();

            if (response.isSuccessful()) {

      /*** HERE response.body contains whole JSON, you need to pass through whole chain to get desire Data. SO Organization class conatains DATA so use it like***/

                datalist = response.body().getOrganization().getData();;

       /--- datalist  is list of Datum array list. now pass that to adapter ---/

                adapter = new ListAdapter(MainActivity.this, datalist);
                listView.setAdapter(adapter);

            } else {
                Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {

        }
    });

【讨论】:

  • 除了代码示例,请添加描述,以便有类似问题的人可以通过阅读您的提示获得上下文。
  • 仍然崩溃。当我跟踪代码流时,它应该可以工作。我真的不知道是什么导致应用崩溃。
  • 问题出在 baseURL 先生。但我认为这是您的解决方案和@MaxExplode 的结合。谢谢你们俩。
【解决方案4】:

我遇到了同样的问题“应用程序崩溃但不显示错误”

但我有以下问题:

我忘了在参数中包含@Body

@Headers({CACHE, AGENT})
@POST("api/check/info")
Call<CallbackInfo> checkInfo(
    Info info
);

并通过添加@Body解决了问题:

@Headers({CACHE, AGENT})
@POST("api-v1/check/info")
Call<CallbackInfo> checkInfo(
    @Body Info info
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多