使用Retrofit2崩溃的Android应用程序没有错误消息

时间:2017-04-15 07:58:48

标签: java android retrofit2

我正在开发一个Android应用程序,需要消耗部署的休息api,现在,在一个heroku实例上...因为我试图整合改造作为http客户端我崩溃了我已经完成了以下内容:

添加了retrofit2作为dependendcy(没有选择最新版本以避免潜在的成熟度问题)

compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'

compile 'com.google.code.gson:gson:2.6.2'

我编写了一个示例Android应用,只是为了检查我的原始应用中是否有错误,使用http://httpbin.org/ip

public interface HttpBinService
{
public static class Response
{
    private String origin;

    public Response(String origin) {
        this.origin = origin;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }
}

@GET("ip")
public Call<Response> getIp ();
}

主要活动

public class MainActivity extends AppCompatActivity {

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

    // Retrofit setup
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://httpbin.org")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    // Service setup
    HttpBinService service = retrofit.create(HttpBinService.class);
    try {
        Call<HttpBinService.Response> call = service.getIp();

        Response<HttpBinService.Response> res = call.execute();

        if (res.isSuccessful()){
            Log.i("PROVARETROFIT", "OK");

            ((TextView)findViewById(R.id.testo)).setText(res.body().getOrigin());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

我没有忘记在我的清单中要求互联网权限

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.spich.provaretrofit">

<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

但我得到的是应用程序意外关闭。如果在调试中执行它,它会正确执行所有步骤,直到调用执行。 LogCat似乎没有说任何有用的东西

04-15 09:48:08.281 6491-6491/? I/art: Late-enabling -Xcheck:jni
04-15 09:48:08.367 6491-6491/? W/System: ClassLoader referenced unknown path: /data/app/it.spich.provaretrofit-1/lib/arm64
04-15 09:48:08.387 6491-6491/it.spich.provaretrofit I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
04-15 09:48:08.577 6491-6491/? I/Process: Sending signal. PID: 6491 SIG: 9

有人知道那里发生了什么吗?

1 个答案:

答案 0 :(得分:1)

不确定为什么logcat没有显示任何有用的东西。但是在执行它时,它会给出android.os.NetworkOnMainThreadException,这可以解释问题。正如他们在评论中所说:尝试使用enque方法和回调,这也允许你摆脱try / catch语句。尝试使用以下内容替换MainActivity中// Service setup之后的代码:

    HttpBinService service = retrofit.create(HttpBinService.class);
    Call<HttpBinService.Response> call = service.getIp();

    call.enqueue(new Callback<HttpBinService.Response>() {
        @Override
        public void onResponse(Call<HttpBinService.Response> call, Response<HttpBinService.Response> response) {

            if (response.isSuccessful()){
                Log.i("PROVARETROFIT", "OK");

//((TextView)findViewById(R.id.testo)).setText(res.body().getOrigin());
                System.out.println(response.body().getOrigin());
            } else {
                System.out.println("Unsuccesfull");
            }
        }

        @Override
        public void onFailure(Call<HttpBinService.Response> call, Throwable t) {
            System.out.println("Call failed");
        }
    });
相关问题