I have an app that has a viewflipper to keep tabs in view at all times throughout the app, and a listview below the tabs. I'm trying to populate the list view with JSON strings from a web server, the web server is feeding JSON strings in the following format: ["X","Y","Z"] So basically, I want to have the list view in list each string on a different row.
The app is launching ok, the list view just appears to be empty. This is the tutorial I used: http://inchoo.net/mobile-development/android-development/simple-android-json-parsing-example-with-output-into-listactivity/ Here is the full code the main launching activity:
public class Activity extends TabActivity implements OnClickListener {
Button doSomething;
TabHost tabHost;
ViewFlipper flipper;
ListView listview;
HttpResponse re;
String json;
JSONObject j;
@SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
@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,this.fetch());
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> fetch()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
URL twitter = new URL(
"JSON.php");
URLConnectio开发者_C百科n tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != 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.toString());
}
}
}
} 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
}
}
Error in Log Cat:
08-25 21:38:15.742: ERROR/InputDispatcher(81): channel '40a14e38 com.app.android/com.app.android.Activity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
Update, here are the values of jArray as reported by Logcat:
08-26 16:49:07.246: VERBOSE/app(472): jarray value: ["Country1","Country2","Country3"]
These are the correct values!
Ok stepping into the loop, log out outputs the following:
08-26 16:59:55.756: WARN/System.err(506): org.json.JSONException: Value Country1 at 0 of type java.lang.String cannot be converted to JSONObject
08-26 16:59:55.756: WARN/System.err(506): at org.json.JSON.typeMismatch(JSON.java:96)
08-26 16:59:55.766: WARN/System.err(506): at org.json.JSONArray.getJSONObject(JSONArray.java:484)
08-26 16:59:55.775: WARN/System.err(506): at com.app.android.Activity.fetch(Activity.java:165)
08-26 16:59:55.775: WARN/System.err(506): at com.app.android.Activity.onCreate(eActivity.java:93)
08-26 16:59:55.786: WARN/System.err(506): at android.app.Activity.performCreate(Activity.java:4397)
08-26 16:59:55.786: WARN/System.err(506): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-26 16:59:55.795: WARN/System.err(506): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
08-26 16:59:55.806: WARN/System.err(506): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
08-26 16:59:55.806: WARN/System.err(506): at android.app.ActivityThread.access$500(ActivityThread.java:122)
08-26 16:59:55.815: WARN/System.err(506): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
08-26 16:59:55.815: WARN/System.err(506): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 16:59:55.826: WARN/System.err(506): at android.os.Looper.loop(Looper.java:132)
08-26 16:59:55.826: WARN/System.err(506): at android.app.ActivityThread.main(ActivityThread.java:4123)
08-26 16:59:55.836: WARN/System.err(506): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 16:59:55.836: WARN/System.err(506): at java.lang.reflect.Method.invoke(Method.java:491)
08-26 16:59:55.846: WARN/System.err(506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-26 16:59:55.855: WARN/System.err(506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-26 16:59:55.855: WARN/System.err(506): at dalvik.system.NativeStart.main(Native Method)
I think your problem could be that your URL is just "JSON.php". You dont specify the server this resides on (http://somewebsite.com/JSON.php). What happens when you set debug points and step through? Whats the value of the twitter URL after its created? My guess is that it cannot reach this "JSON.php".
EDIT: Try this:
Instead of using
JSONObject jobj = jArray.getJSONObject(i);
Try
String country = jArray.getString(i);
listItems.add(country);
精彩评论