hereからデータを取得し、それを私のアプリに表示したいと思います。Androidのリストビューでウェブページを表示
アクティビティが開始されたときにリストビューが空になっていて、追加する必要があるかどうかが正確ではないので、私はかなり固執しています。本当に助けていただければ幸いです。
public class Details extends AppCompatActivity {
private static final String KEY_LINK = "link";
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
new GetData().execute();
}
private class GetData extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... uri) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String link = in.getStringExtra(KEY_LINK);
String info = null;
final String QUERY = link;
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority("www.minerva.shef.ac.uk")
.appendPath("minerva")
.appendPath("med")
.appendPath("includes")
.appendPath("inc_news_details.php")
.appendQueryParameter("id", QUERY);
try {
URL url = new URL(builder.build().toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
if(urlConnection.getResponseCode() == HttpsURLConnection.HTTP_OK){
Log.d("Hello,", "Spongebob!");
// Do normal input or output stream reading
}
else {
// See documentation for more info on response handling
}
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
info = buffer.toString();
List<String> details = new ArrayList<String>(Arrays.asList(info));
mAdapter = new ArrayAdapter<String>(getApplication(), R.layout.list_item, R.id.link, details);
} catch (IOException e) {
Log.e("Hi", "This is Patrick");
return null;
}
return null;
}
@Override
protected void onPostExecute(String result) {
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(mAdapter);
}
}
}
あなたが求めていることは明確ではありません。私がやっているのは、URLのListView(サイト名や説明を示す)を持つフラグメントを持つことです。それらのいずれかをクリックすると、渡されたURLを示すWebViewを持つ別のフラグメントにスワップします。 –
ああ、私はすでにそれを作りました。私が今したいのは、ユーザーがクリックすると私のアプリにhttps://www.minerva.shef.ac.uk/minerva/med/includes/inc_news_details.php?id=23165のコンテンツを表示することです。 –
...あなたを止めるのは何ですか? –