Android应用程序:从web到listview的JSON数组
我只是想从网上获得一个简单的JSON数组,格式如下:[“Country1”、“Country2”、“Country3”],然后在我的android应用程序中将该数组用作列表视图。我并没有被困在如何制作这个JSON数组上,我只是对如何将它放入应用程序中的listview感到困惑 我尝试了一些不同的教程,但没有一个使用与我相同的布局 我的应用程序使用viewflipper,在整个应用程序中始终保持基于选项卡的布局处于视图中,因此所有教程似乎都无法处理我的布局 非常感谢您的帮助 编辑: 这里有一些代码,是的,我想从web服务解析它并在listview中显示它Android应用程序:从web到listview的JSON数组,android,json,listview,Android,Json,Listview,我只是想从网上获得一个简单的JSON数组,格式如下:[“Country1”、“Country2”、“Country3”],然后在我的android应用程序中将该数组用作列表视图。我并没有被困在如何制作这个JSON数组上,我只是对如何将它放入应用程序中的listview感到困惑 我尝试了一些不同的教程,但没有一个使用与我相同的布局 我的应用程序使用viewflipper,在整个应用程序中始终保持基于选项卡的布局处于视图中,因此所有教程似乎都无法处理我的布局 非常感谢您的帮助 编辑: 这里有一些代码
public class Activity extends TabActivity implements OnClickListener {
Button doSomething;
TabHost tabHost;
ViewFlipper flipper;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout_1);
doSomething = (Button) findViewById(R.id.btn_do_something);
doSomething.setOnClickListener(this);
flipper = (ViewFlipper) findViewById(R.id.layout_tab_one);
listview = (ListView) findViewById(R.id.listview);
@SuppressWarnings("unchecked")
ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,fetchTwitterPublicTimeline());
//ListAdapter adapter = new SimpleAdapter(this, this.fetchTwitterPublicTimeline() , R.layout.main, new int[] { R.id.item_title, R.id.item_subtitle });
listview.setAdapter(adapter);
flipper.setOnClickListener(this);
String tabname1 = getString(R.string.tabexample_tab1);
String tabname2 = getString(R.string.tabexample_tab2);
String tabname3 = getString(R.string.tabexample_tab3);
String tabname4 = getString(R.string.tabexample_tab4);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setContent(R.id.layout_tab_one).setIndicator(tabname1));
tabHost.addTab(tabHost.newTabSpec("tab2").setContent(R.id.layout_tab_two).setIndicator(tabname2));
tabHost.addTab(tabHost.newTabSpec("tab3").setContent(R.id.layout_tab_three).setIndicator(tabname3));
tabHost.addTab(tabHost.newTabSpec("tab4").setContent(R.id.layout_tab_four).setIndicator(tabname4));
tabHost.setCurrentTab(0);
listview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
flipper.showNext();
}});
}
public ArrayList<String> fetchTwitterPublicTimeline()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
URL twitter = new URL(
"JSON.php");
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
listItems.add(jo.getString(""));
}
}
} 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 listItems;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
公共类活动扩展TabActivity实现OnClickListener{
按钮剂量测定法;
TabHost-TabHost;
视图翻转器翻转器;
列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.TableLayout_1);
doSomething=(按钮)findviewbyd(R.id.btn\u do\u something);
doSomething.setOnClickListener(this);
flipper=(ViewFlipper)findViewById(R.id.layout\u tab\u one);
listview=(listview)findViewById(R.id.listview);
@抑制警告(“未选中”)
ListAdapter=new ArrayAdapter(这是android.R.layout.simple_list_item_1,fetchTwitterPublicTimeline());
//ListAdapter=new SimpleAdapter(this,this.fetchTwitterPublicTimeline(),R.layout.main,new int[]{R.id.item_title,R.id.item_subtitle});
setAdapter(适配器);
flipper.setOnClickListener(这个);
String tabname1=getString(R.String.tabexample\u tab1);
String tabname2=getString(R.String.tabexample\u tab2);
String tabname3=getString(R.String.tabexample\u tab3);
String tabname4=getString(R.String.tabexample\u tab4);
tabHost=getTabHost();
tabHost.addTab(tabHost.newTabSpec(“tab1”).setContent(R.id.layout_tab_one).setIndicator(tabname1));
tabHost.addTab(tabHost.newTabSpec(“tab2”).setContent(R.id.layout_tab_two).setIndicator(tabname2));
tabHost.addTab(tabHost.newTabSpec(“tab3”).setContent(R.id.layout_tab_three).setIndicator(tabname3));
tabHost.addTab(tabHost.newTabSpec(“tab4”).setContent(R.id.layout_tab_four).setIndicator(tabname4));
tabHost.setCurrentTab(0);
setOnItemClickListener(新的OnItemClickListener(){
公共视图单击(适配器视图a、视图v、内部位置、长id){
flipper.showNext();
}});
}
公共ArrayList fetchTwitterPublicTimeline()
{
ArrayList listItems=新的ArrayList();
试一试{
URL twitter=新URL(
“JSON.php”);
URLConnection tc=twitter.openConnection();
BufferedReader in=新的BufferedReader(新的InputStreamReader(
tc.getInputStream());
弦线;
而((line=in.readLine())!=null){
JSONArray ja=新JSONArray(线);
对于(int i=0;i
更新:我仍然不能让它工作,任何想法在下面的代码中有什么错误
public class Activity extends TabActivity implements OnClickListener {
Button doSomething;
TabHost tabHost;
ViewFlipper flipper;
ListView listview;
HttpResponse re;
String json;
JSONObject j;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
//final String TAG = "MainActivity";
//final String URL = "JSON.php";
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout_1);
final String[] listItems = new String[] { };
/*========================================
// JSON object to hold the information, which is sent to the server
JSONObject jsonObjSend = new JSONObject();
try {
// Add key/value pairs
jsonObjSend.put("key_1", "value_1");
jsonObjSend.put("key_2", "value_2");
// Add a nested JSONObject (e.g. for header information)
JSONObject header = new JSONObject();
header.put("deviceType","Android"); // Device type
header.put("deviceVersion","2.0"); // Device OS version
header.put("language", "es-es"); // Language of the Android client
jsonObjSend.put("header", header);
// Output the JSON object we're sending to Logcat:
Log.i(TAG, jsonObjSend.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
// Send the HttpPostRequest and receive a JSONObject in return
JSONObject jsonObjRecv = HTTPClient.SendHttpPost(URL, jsonObjSend);
String temp = jsonObjRecv.toString();
/*==============================================*/
doSomething = (Button) findViewById(R.id.btn_do_something);
doSomething.setOnClickListener(this);
flipper = (ViewFlipper) findViewById(R.id.layout_tab_one);
listview = (ListView) findViewById(R.id.listview);
/* try {
JSONArray array = jsonObjRecv.getJSONArray(""); //(JSONArray) new JSONTokener(json).nextValue();
String[] stringarray = new String[array.length()];
for (int i = 0; i < array.length(); i++) {
stringarray[i] = array.getString(i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringarray);
listview.setAdapter(adapter);
} catch (JSONException e) {
// handle JSON parsing exceptions...
}*/
//@SuppressWarnings("unchecked")
ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,fetchTwitterPublicTimeline());
//ListAdapter adapter = new SimpleAdapter(this, this.fetchTwitterPublicTimeline() , R.layout.main, new int[] { R.id.item_title, R.id.item_subtitle });
listview.setAdapter(adapter);
flipper.setOnClickListener(this);
String tabname1 = getString(R.string.tabexample_tab1);
String tabname2 = getString(R.string.tabexample_tab2);
String tabname3 = getString(R.string.tabexample_tab3);
String tabname4 = getString(R.string.tabexample_tab4);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setContent(R.id.layout_tab_one).setIndicator(tabname1));
tabHost.addTab(tabHost.newTabSpec("tab2").setContent(R.id.layout_tab_two).setIndicator(tabname2));
tabHost.addTab(tabHost.newTabSpec("tab3").setContent(R.id.layout_tab_three).setIndicator(tabname3));
tabHost.addTab(tabHost.newTabSpec("tab4").setContent(R.id.layout_tab_four).setIndicator(tabname4));
tabHost.setCurrentTab(0);
listview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
flipper.showNext();
}});
}
public ArrayList<String> fetchTwitterPublicTimeline()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
URL twitter = new URL(
"JSON.php");
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line = null;
//make sure youe String line is completely filled after that..
if (!line.equals(null) && !line.equals("") && line.startsWith("["))
{
JSONArray jArray = new JSONArray(line);
for (int i = 0; i < jArray.length(); i++)
{
JSONObject jobj = jArray.getJSONObject(i);
// also make sure you get the value from the jsonObject using some key
// like, jobj.getString("country");
listItems.add(jobj.getString(""));
}
}
} 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 listItems;
}
/* public ArrayList<String> fetchTwitterPublicTimeline()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
URL twitter = new URL(
"JSON.php");
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
//make sure youe String line is completely filled after that..
if (!line.equals(null) && !line.equals("") && line.startsWith("["))
{
JSONArray jArray = new JSONArray(line);
for (int i = 0; i < jArray.length(); i++)
{
JSONObject jobj = jArray.getJSONObject(i);
// also make sure you get the value from the jsonObject using some key
// like, jobj.getString("country");
listItems.add(jobj.getString(""));
}
}
/* String line;
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
listItems.add(jo.getString(""));
}
}
} 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 listItems;
}*/
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
公共类活动扩展TabActivity实现OnClickListener{
按钮剂量测定法;
TabHost-TabHost;
视图翻转器翻转器;
列表视图列表视图;
HttpResponse-re;
字符串json;
JSONObject j;
@抑制警告(“弃用”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//最后一个字符串TAG=“MainActivity”;
//最终字符串URL=“JSON.php”;
super.onCreate(savedInstanceState);
setContentView(R.layout.TableLayout_1);
最终字符串[]listItems=新字符串[]{};
/*========================================
//JSON对象来保存发送到服务器的信息
JSONObject jsonObjSend=新的JSONObject();
试一试{
//添加键/值对
jsonObjSend.put(“键1”、“值1”);
jsonObjSend.put(“key_2”,“value_2”);
//添加嵌套的JSONObject(例如,用于标题信息)
JSONObject头=新的JSONObject();
header.put(“deviceType”、“Android”);//设备类型
header.put(“deviceVersion”,“2.0”);//设备操作系统版本
header.put(“language”、“es”);//Android客户端的语言
jsonObjSend.put(“header”,header);
//输出要发送到Logcat的JSON对象:
Log.i(TAG,jsonObjSend.toString(2));
}捕获(JSONException e){
e、 printStackTrace();
}
//发送HttpPostRequest并接收JSONObject作为回报
JSONObject jsonObjRecv=HTTPClient.SendHttpPost(URL,jsonObjSend);
字符串temp=jsonObjRecv.toString();
/*==============================================*/
doSomething=(按钮)findviewbyd(R.id.btn\u do\u something);
doSomething.setOnClickListener(this);
flipper=(ViewFlipper)findViewById(R.id.layout\u tab\u one);
listview=(listview)findViewById(R.id.listview);
/*试一试{
JSONArray数组=jsonObjRecv.getJSONArray(“”;/(JSONArray)新的JSONTokener(json).nextValue();
String[]stringarray=新字符串[array.length()];
对于(int i=0;iListView list = (ListView) findViewById(...);
String json = "[\"Country1\",\"Country2\",\"Country3\"]";
try {
JSONArray array = (JSONArray) new JSONTokener(json).nextValue();
String[] stringarray = new String[array.length()];
for (int i = 0; i < array.length(); i++) {
stringarray[i] = array.getString(i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringarray);
list.setAdapter(adapter);
} catch (JSONException e) {
// handle JSON parsing exceptions...
}