2016-12-06 76 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 { 
... 
}