0

我在 udacity 教程之后做了一个简单的应用程序:https ://classroom.udacity.com/courses/ud853 。源代码在这里:https ://github.com/ionutincau/Vremea 。

如果我通过 USB 调试模式(Android Studio 中的运行选项)安装应用程序,它工作正常,但如果我“构建 APK”并安装构建的 apk,应用程序崩溃。

我有 Internet 连接,但是当我尝试从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 文件安装应用程序时才会出现错误

设置活动

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
4

1 回答 1

0

创建活动的问题是因为您需要公开他们的课程。

public class SettingsActivity extends Activity {
}

对于 filenotfound,您可以在浏览器中打开链接并查看实际响应: {"cod":"502","message":"Error: Not found city"}

您需要对其进行配置以获得正确的响应,但您还应该通过检查 urlConnection.getResponseCode() 与 HttpURLConnection.HTTP_OK 来处理错误响应。如果失败,您将获得有关 urlConnection.getResponseMessage() 或流 urlConnection.getErrorStream() 的更多信息。

if (urlConnection.getResposeCode() == HttpURLConnection.HTTP_OK) {
...
} else {
...
}
于 2016-12-07T16:13:25.243 回答