我有一些在线存储的JSON数据,我想从URL中获取它并将其显示在Android Studio的tablelayout中。目前我所做的代码似乎没有起作用。当我点击应用程序上的按钮时,显示的所有内容都是" []"而不是arraylist所需的信息。
有什么想法吗?
public class MainActivity extends AppCompatActivity {
TableLayout tabLay;
TableRow tabRow;
TextView label1, label2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLay = (TableLayout) findViewById(R.id.tableLayout);
tabRow = (TableRow) findViewById(R.id.tableRow);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
这个方法应该从JSON数据中获取人的属性,将它们放在数组列表中,然后返回数组列表
public ArrayList<String> getPerson() {
ArrayList<String> peopleArray = new ArrayList<String>();
try {
URL url = new URL("http://some_url_with_JSON_data");
HttpURLConnection urlCON = (HttpURLConnection) url.openConnection();
InputStreamReader isr = new InputStreamReader(urlCON.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
JSONArray jArray = new JSONArray(line);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = (JSONObject) jArray.get(i);
peopleArray.add(jObject.getString("FirstName"));
peopleArray.add(jObject.getString("LastName"));
peopleArray.add(jObject.getString("Address"));
}
}
} catch (SecurityException e) {
throw new ArrayIndexOutOfBoundsException();
} catch (Exception e) {
e.printStackTrace();
}
return peopleArray;
}
此方法应该接收返回的ArrayList(peopleArray)并将其添加到tablerow。目前所有显示的都是&#34; []&#34;
public void table() {
String array = getPerson().toString();
label1 = new TextView(this);
label1.setText(array);
tabRow = new TableRow(this);
tabRow.addView(label1);
tabLay.addView(tabRow);
}
// this method calls the table method when the "get person" button is clicked
public void onClick(View v){
table();
}
}
在Manifest&#34; internet&#34;已启用。
<uses-permission android:name="android.permission.INTERNET"/>
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="@string/name"
tools:showIn="@layout/activity_main"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="55dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Person"
android:id="@+id/button"
android:onClick="onClick" />
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tableLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tableRow"></TableRow>
</TableLayout>
</LinearLayout>
以下是来自网址
的JSON数据的摘录[
{
"FirstName": "Jimmy",
"LastName": "Cricket",
"Address": "Arizona",
},
{
"FirstName": "Freddy",
"LastName": "Foxx",
"Address": "Washington State",
},
{
"FirstName": "Louis",
"LastName": "Jones",
"Address": "Texas",
}
]