Java 安卓应用程序改型2崩溃,无错误消息
我正在开发一个android应用程序,该应用程序需要使用部署在heroku实例上的rest API…由于我在尝试将改造集成为http客户端时遇到崩溃,我已完成以下操作: 作为Dependency添加了Reformation2(未选择最新版本以避免潜在的成熟度问题) 我编写了一个示例android应用程序,只是为了检查我的原始应用程序中是否存在错误,使用Java 安卓应用程序改型2崩溃,无错误消息,java,android,retrofit2,Java,Android,Retrofit2,我正在开发一个android应用程序,该应用程序需要使用部署在heroku实例上的rest API…由于我在尝试将改造集成为http客户端时遇到崩溃,我已完成以下操作: 作为Dependency添加了Reformation2(未选择最新版本以避免潜在的成熟度问题) 我编写了一个示例android应用程序,只是为了检查我的原始应用程序中是否存在错误,使用 有人知道那里发生了什么吗?不知道为什么logcat没有显示任何有用的东西。但在执行时,它给出了一个android.os.NetworkOnMai
有人知道那里发生了什么吗?不知道为什么logcat没有显示任何有用的东西。但在执行时,它给出了一个
android.os.NetworkOnMainThreadException
,这可以解释问题。因此,正如他们在评论中所说:尝试使用带有回调的enque方法,这也允许您摆脱try/catch语句。尝试将MainActivity中//服务设置
之后的代码替换为:
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");
}
});
HttpBinService服务=改装.create(HttpBinService.class);
Call=service.getIp();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
Log.i(“PROVARETROFIT”、“OK”);
//((TextView)findviewbyd(R.id.testo)).setText(res.body().getOrigin());
System.out.println(response.body().getOrigin());
}否则{
System.out.println(“未成功”);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
System.out.println(“调用失败”);
}
});
删除对谷歌gson的依赖性您如何判断/确保改装会导致崩溃?尝试使用类似calback的服务。enque(新回调…)是否使用Instant Run by chance?如果是,请尝试禁用它。我添加了gson depencency字符串来修复以前存在的问题。Http请求永远不会执行,也不会同步(使用execute())或异步(使用enqueue()),在调试中一步一步地执行应用程序,这两种方法都会导致“信号9”,没有任何异常或错误消息,从.baseUrl(“)到.baseUrl(“)太棒了!嗯,网络呼叫不应该在主线程上进行,因为如果这样做了,它只会冻结整个应用程序。使用此回调结构将强制在单独的线程上执行,从而允许应用程序继续执行其工作。我们基本上被迫在一个单独的威胁上执行网络呼叫,这迫使我们不能生成因此而冻结的应用程序。您以前的代码在主线程上执行了网络调用,这会引发此NetworkOnMainThreadException。
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>
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
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");
}
});