Извините, но я действительно новичок в Android. Я следую онлайновому учебнику для чайников и следую инструкциям учебника, я попытался написать простое приложение, которое читает канал RSS и помещает все элементы (заголовок + ссылка) в ListView.
В DebugВ режиме я обнаружил, что сбой происходит в строке 73
return url.openConnection().getInputStream();
Это мое основное занятие (извините за итальянский комментарий и несколько журналов, я использовал его для отладки)
package com.example.readfrominternet;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
//definiamo le variabili
ListView lvRss;
ArrayList<String> titoli;
ArrayList<String> link;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//assegniamo le variabili agli oggetti
lvRss = findViewById(R.id.lvRss);
titoli = new ArrayList<String>();
link = new ArrayList<String>();
//specifichiamo cosa fare quando clicchiamo sul singolo item
lvRss.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Log.v("MAIN", "Abbiamo cliccato sul singlo item, riga 54");
Uri uri = Uri.parse(link.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Log.v("MAIN", "Abbiamo aperto il link sul browser, riga 58");
}
});
new ProcessInBackground().execute();
Log.v("MAIN", "Abbiamo eseguito il processo in background, riga 64");
}
//definiamo il metodo per fare lo streaming del file
public InputStream getInputStream(URL url)
{
Log.v("MAIN", "Siamo all'interno del getInputStream, riga 70");
try
{
return url.openConnection().getInputStream();
}
catch(Exception e)
{
return null;
}
}
public class ProcessInBackground extends AsyncTask<Integer, Void, Exception>
{
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
Exception exception = null;
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog.setMessage("Busy loading RSS feed, please wait!");
progressDialog.show();
}
@Override
protected Exception doInBackground(Integer... params )
{
Log.v("MAIN", "Siamo nel doInBackground, riga 99");
try
{
//servono per il parsing
URL url = new URL("http://feeds.news24.com/articles/fin24/tech/rss");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8"); //UTF_8 è il tipo di encoding utilizzato per il testo
Log.v("MAIN", "Siamo nel setting dello streaming, riga 109");
//serve a capire quando sono all'interno dell'item che voglio leggere
boolean insideItem = false;
// la variabile eventType ci aiuta a capire se il tag che stiamo leggendo è un tag di apertura o di chiusura, etc.
int eventType = xpp.getEventType();
Log.v("MAIN", "Recuperiamo il tipo dell'evento, riga 115");
while(eventType != XmlPullParser.END_DOCUMENT)
{
Log.v("MAIN", "Siamo dentro il while, riga 119");
if(eventType == XmlPullParser.START_TAG)
{
if(xpp.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title"))
{
if(insideItem)
{
titoli.add(xpp.nextText());
}
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if(insideItem)
{
link.add(xpp.nextText());
}
}
}
else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item"))
{
insideItem = false;
}
eventType = xpp.next();
}
}
catch(XmlPullParserException e)
{
exception = e;
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT);
}
catch(MalformedURLException e)
{
exception = e;
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT);
}catch(IOException e)
{
exception = e;
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT);
}
return exception;
}
@Override
protected void onPostExecute(Exception s)
{
super.onPostExecute(s);
Log.v("MAIN", "Siamo nell'onPostExecute, riga 171");
//settiamo la lista con gli articoli letti
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, titoli);
lvRss.setAdapter(adapter);
Log.v("MAIN", "Settimao l'adapter della lista, riga 175");
progressDialog.dismiss();
Log.v("MAIN", "Dismettiamo il progress dialog, riga 178");
}
}
}
Этоэто ошибка, которую я прочитал в окнах Logcat:
2019-10-21 18:08:42.873 14570-14570/com.example.readfrominternet E/WindowManager: android.view.WindowLeaked: Activity com.example.readfrominternet.MainActivity has leaked window DecorView@5ba2a72[MainActivity] that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:511)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:346)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:329)
at com.example.readfrominternet.MainActivity$ProcessInBackground.onPreExecute(MainActivity.java:92)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:648)
at android.os.AsyncTask.execute(AsyncTask.java:595)
at com.example.readfrominternet.MainActivity.onCreate(MainActivity.java:63)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Я правильно использовал разрешение для android-manifest.
Как я могу ее разрешить?
Много танков вавансовый.