JSON URLリンクからJSON結果を表示しようとしています。現在、ロードすると何も表示されず、空白のページしか表示されません。 This is the source where I got information about JSON。 ANDROID、WebサーバーからJSONデータを解析してListViewに表示
この
は私のコードです:public class DVLAresult extends AppCompatActivity {
public class DVLAlist extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_dvlaresult);
setListAdapter(new ArrayAdapter(
this,android.R.layout.simple_list_item_2,
this.populate()));
}
private ArrayList<String> populate() {
ArrayList<String> items = new ArrayList<String>();
TextView newtext = (TextView) findViewById(R.id.view_number);
try {
URL url = new URL
("https://dvlasearch.appspot.com/DvlaSearch?licencePlate=mt09nks&apikey=DvlaSearchDemoAccount");
HttpURLConnection urlConnection =
(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// gets the server json data
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String next;
while ((next = bufferedReader.readLine()) != null) {
JSONArray ja = new JSONArray(next);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
items.add(jo.getString("text"));
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return items;
}
}
}
そして、これは私のXMLファイルsimple_list_2.xml
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingStart"
android:paddingEnd="?attr/listPreferredItemPaddingEnd">
<TextView android:id="@id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="@id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text1"
android:layout_alignStart="@id/text1"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
<TextView android:id="@id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text2"
android:layout_alignStart="@id/text2"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
... Continue up to text18, because I have 18 fields.
</TwoLineListItem>
であり、これは私が似て発生したために起こる主なXMLのリストビュー
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_above="@+id/button2"
android:layout_below="@+id/view_number" />
あなたは、一般的なスレッドと同じスレッド上の要求を行うことができません。 'Asynctask'を拡張するクラスを使用するか、Retrofit、Volleyなどのライブラリを使用してください。 –
リクエストはメインアクティビティから実行され、これは第2のアクティビティです。だから私は値を設定し、この値に従ってURLを変更します。しかし現在、結果を表示する必要はありません。私は私の質問を更新し、私は情報を持っているソースを置くだろうJSON –
あなたは例外を得ることができますか?あなたがメインスレッドでネットワーク接続を行っているようです...とにかく問題がある場合は、コード内のすべてのステップでデバッグメッセージを出力する必要があります(Log.i( "json"、 "method called populate()" ))。これは、難しい部分を見つけるのに役立ちます。あなたは常にあなたが有効な応答を得るためにURL接続のようなものから結果を印刷する必要があります。 – Luftbaum