2017-04-15 108 views
0

我正在開發一個Android應用程序需要消耗休息apis部署,現在,在一個heroku實例...因爲我得到崩潰隨時我嘗試集成改造爲http客戶端我已經做了以下內容:Android版應用程序與Retrofit2崩潰沒有錯誤信息

新增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 

有人想到發生了什麼嗎?

+0

刪除谷歌GSON依賴 –

+0

你是怎麼計算/是肯定的改造正在崩潰?嘗試使用calback like services.enque(new Callback ..) – DevKRos

+0

您是否正在使用Instant Run?如果你是,請嘗試禁用它。 –

回答

1

不知道爲什麼logcat不顯示任何有用的東西。但是執行它時,它會給出一個android.os.NetworkOnMainThreadException,這可以解釋問題。正如他們在評論中所說:嘗試使用帶回調的enque方法,這也可以讓你擺脫try/catch語句。嘗試用替換// Service setup後的代碼在你的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"); 
     } 
    }); 
+0

很多謝謝, 有用!!!但我仍然看不到這一點....爲什麼這有效? – sscnapoli1926

+0

太棒了!那麼,網絡調用不應該在主線程上完成,因爲如果這樣做完全凍結了整個應用程序。使用此回調結構強制它在單獨的線程上執行,從而允許應用程序繼續執行它的工作。 我們基本上被迫在單獨的威脅上執行網絡調用,這迫使我們不要生成因此而凍結的應用程序。您之前的代碼在主線程上執行了網絡調用,這會引發此NetworkOnMainThreadException。 – Alex