2016-12-06 47 views
0

我在udacity教程:https://classroom.udacity.com/courses/ud853後有一個簡單的應用程序。 源代碼在這裏:https://github.com/ionutincau/Vremea僅當我安裝APK時發生錯誤

如果我通過USB調試模式(在Android Studio中運行選項)安裝應用程序,它工作正常,但如果我「構建APK」並安裝構建的apk,應用程序崩潰。

我有互聯網連接,但是當我嘗試從http://api.openweathermap.org獲取數據時,我收到了URL Connection Error java.io.FileNotFoundException。而且,當我嘗試打開設置,我得到:

FATAL EXCEPTION: main 
Process: com.example.ionut.vremea2, PID: 6850 
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ionut.vremea2/com.example.ionut.vremea2.SettingsActivity}: java.lang.IllegalAccessException: 

只有安裝從apk文件的應用程序,我得到了錯誤

SettingsActivity

class SettingsActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 
    } 
} 

我使用此代碼從網上獲取數據:

protected String[] doInBackground(String... params) { 
     if (params.length == 0) { 
      return null; 
     } 

     HttpURLConnection urlConnection = null; 
     BufferedReader reader = null; 
     String forecastJsonStr = null; 

     String format = "json"; 
     String units = "metric"; 
     int numDays = 7; 
     String api_key = "my_key"; // I have a valid code in my app 

     try { 
      // Construct the URL for the OpenWeatherMap query 
      final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?"; 
      final String QUERY_PARAM = "id"; 
      final String FORMAT_PARAM = "mode"; 
      final String UNITS_PARAM = "units"; 
      final String DAYS_PARAM = "cnt"; 
      final String APPID_PARAM = "APPID"; 

      // Construct the URL for the OpenWeatherMap query 
      Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon() 
        .appendQueryParameter(QUERY_PARAM, params[0]) 
        .appendQueryParameter(FORMAT_PARAM, format) 
        .appendQueryParameter(UNITS_PARAM, units) 
        .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays)) 
        .appendQueryParameter(APPID_PARAM, api_key) 
        .build(); 

      URL url = new URL(builtUri.toString()); 

      // Create the request to OpenWeatherMap, and open the connection 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.connect(); 

      // Read the input stream into a String 
      InputStream inputStream = urlConnection.getInputStream(); 
      // ... do something 
+0

顯示相關代碼。 –

+0

源代碼在這裏:https://github.com/ionutincau/Vremea。 – WhiteShadow

回答

0

該pro因爲你需要公開他們的課程,所以創建這些活動是不明智的。

public class SettingsActivity extends Activity { 
} 

對於filenotfound你可以打開瀏覽器中的鏈接,看到了實際的迴應: {「鱈魚」:「502」,「消息」:「錯誤:未找到城市」}

您需要將其配置爲獲得正確的響應,但您也應該通過針對HttpURLConnection.HTTP_OK檢查urlConnection.getResponseCode()來處理錯誤響應。如果發生故障,您可以在urlConnection.getResponseMessage()或流urlConnection.getErrorStream()上獲得更多信息。

if (urlConnection.getResposeCode() == HttpURLConnection.HTTP_OK) { 
... 
} else { 
... 
}