【发布时间】:2016-07-13 07:39:22
【问题描述】:
我正在尝试显示来自 JSON url 链接的 JSON 结果。目前,当我加载时,它什么都不显示,只是空白页。 This is the source where I got information about JSON.
这是我的代码:
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
<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" />
【问题讨论】:
-
您不能在与一般线程相同的线程上发出请求。使用
extends Asynctask的类或使用 Retrofit、Volley 等库。 -
请求将从主要活动完成,这是第二个活动。所以我会设置一个值,并根据这个值改变 URL。但目前不想显示任何结果。我会更新我的问题,我会在我有 JSON 信息的地方放一个来源
-
您没有收到异常吗?看起来你正在主线程中进行网络连接......无论如何,如果你有这样的问题,你应该在你的代码中的每一步打印调试消息(Log.i(“json”,“调用方法populate()” ))。这将帮助您找到棘手的部分。您应该始终打印来自 URL 连接之类的结果,以确保您得到有效的响应。
-
完全没有异常错误。或者我做错了什么
-
@J.FOG。您确定您从服务器收到响应。我不是专家,但我最近做了一些相关的工作,我应该能够毫不费力地将数据显示在滚动视图中。