API -Anfrage: Arbeiten Sie nicht bei Android Studio
Posted: 27 Jan 2025, 08:19
Immer wenn ich versuche, eine API-Anfrage von „http://api.openweathermap.org/data/2.5/weather?q=London“ zu stellen
kann der Code die Daten nicht abrufen.
Dies funktioniert jedoch problemlos mit anderen URLs. Hier ist die Haupt-Java-Datei.
Ich habe der Manifestdatei die erforderlichen Berechtigungen hinzugefügt.
Außerdem kann ich den Fehler im Logcat und im Programm nicht herausfinden.
Jede Hilfe wäre hilfreich geschätzt werden.
kann der Code die Daten nicht abrufen.
Dies funktioniert jedoch problemlos mit anderen URLs. Hier ist die Haupt-Java-Datei.
Ich habe der Manifestdatei die erforderlichen Berechtigungen hinzugefügt.
Code: Select all
package thebestone.vicky.jsondemo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
class GetData extends AsyncTask{
@Override
protected String doInBackground(String... strings) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try{
url = new URL(strings[0]);
urlConnection = (HttpURLConnection)url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while(data != -1){
char ch = (char) data;
result = result + ch;
data = reader.read();
}
return result;
}catch (Exception e) {
e.printStackTrace();
return "Failed";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("DATA FETCHED: ", s);
}
}
public void onClick(View view){
GetData task = new GetData();
task.execute("http://api.openweathermap.org/data/2.5/weather?q=London");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
< /code>
logcat: < /p>
08-18 19:05:10.478 2380-4307/thebestone.vicky.jsondemo W/System.err: java.io.FileNotFoundException: http://api.openweathermap.org/data/2.5/weather?q=London
08-18 19:05:10.480 2380-4307/thebestone.vicky.jsondemo W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
08-18 19:05:10.480 2380-4307/thebestone.vicky.jsondemo W/System.err: at thebestone.vicky.jsondemo.MainActivity$GetData.doInBackground(MainActivity.java:33)
08-18 19:05:10.480 2380-4307/thebestone.vicky.jsondemo W/System.err: at thebestone.vicky.jsondemo.MainActivity$GetData.doInBackground(MainActivity.java:19)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
08-18 19:05:10.481 2380-4307/thebestone.vicky.jsondemo W/System.err: at java.lang.Thread.run(Thread.java:761)
08-18 19:05:10.481 2380-2380/thebestone.vicky.jsondemo I/DATA FETCHED:: Failed
Jede Hilfe wäre hilfreich geschätzt werden.