package com.maptower;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class FragmentA extends Fragment {
private static String url = "http://192.168.2.150/54_tower/Json_Tower.php";
//JSON Node Names
private static final String J_NAME = "t_name";
private static final String J_NUM = "t_num";
private static final String J_FLOOR = "t_floor";
private static final String J_CLASS = "t_room_class";
private static final String J_TEACHER = "t_room_teacher";
private static final String J_TOILET = "t_toilet";
private static final String J_MAP = "t_map";
private static final String D_NAME = "NAME";
private static final String D_NUM = "NUM";
private static final String D_FLOOR = "FLOOR";
private static final String D_CLASS = "CLASS";
private static final String D_TEACHER = "TEACHER";
private static final String D_TOILET = "TOILET";
private static final String D_MAP = "MAP";
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
JSONArray cities = null;
ListView list;
public static FragmentA newInstance(int page, String title) {
FragmentA fragmentFirst = new FragmentA();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_tower, container, false);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
//ListView lisView1 = (ListView) rootView.findViewById(R.id.listView1);
//new JSONParse().execute();
/*
* lisView1.setOnItemClickListener(new OnItemClickListener() {
*
* public void onItemClick(AdapterView<?> parent, View view, int
* position, long id) {
*
* Intent i = new Intent(getActivity(), Map.class); startActivity(i);
*
* } });
*/
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
oslist = new ArrayList<HashMap<String, String>>();
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
private JSONObject json;
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected JSONObject doInBackground(String... args) {
json = jParser.getJSONFromUrl(url);
Log.d("All JSon: ", json.toString());
return json;
}
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
cities = json.getJSONArray("cities");
Log.d("TAG JSON", "" + cities.toString());
for (int i = 0; i < cities.length(); i++) {
JSONObject c = cities.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put(D_NAME, c.getString(J_NAME));
map.put(D_NUM, c.getString(J_NUM));
map.put(D_FLOOR, c.getString(J_FLOOR));
map.put(D_CLASS, c.getString(J_CLASS));
map.put(D_TEACHER, c.getString(J_TEACHER));
map.put(D_TOILET, c.getString(J_TOILET));
map.put(D_MAP, c.getString(J_MAP));
oslist.add(map);
list = (ListView)getView().findViewById(R.id.listView1);
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.tower_column,
new String[] {D_NAME,D_NUM,D_FLOOR,D_CLASS,D_TEACHER,D_TOILET,D_MAP},new int[] { R.id.Name,R.id.Num,R.id.Floor,R.id.Room_Class,R.id.Room_Teacher,R.id.Toilet});
list.setAdapter(adapter);
// list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(getActivity(), "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
// }
// });
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
ส่วนนี้คือ Logcat Code
12-25 00:16:48.912: W/System.err(27155): org.json.JSONException: No value for cities
12-25 00:16:48.912: W/System.err(27155): at org.json.JSONObject.get(JSONObject.java:355)
12-25 00:16:48.922: W/System.err(27155): at org.json.JSONObject.getJSONArray(JSONObject.java:549)
12-25 00:16:48.922: W/System.err(27155): at com.maptower.FragmentA$JSONParse.onPostExecute(FragmentA.java:139)
12-25 00:16:48.922: W/System.err(27155): at com.maptower.FragmentA$JSONParse.onPostExecute(FragmentA.java:1)
12-25 00:16:48.922: W/System.err(27155): at android.os.AsyncTask.finish(AsyncTask.java:632)
12-25 00:16:48.922: W/System.err(27155): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-25 00:16:48.922: W/System.err(27155): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
12-25 00:16:48.922: W/System.err(27155): at android.os.Handler.dispatchMessage(Handler.java:102)
12-25 00:16:48.922: W/System.err(27155): at android.os.Looper.loop(Looper.java:149)
12-25 00:16:48.922: W/System.err(27155): at android.app.ActivityThread.main(ActivityThread.java:5268)
12-25 00:16:48.922: W/System.err(27155): at java.lang.reflect.Method.invokeNative(Native Method)
12-25 00:16:48.922: W/System.err(27155): at java.lang.reflect.Method.invoke(Method.java:515)
12-25 00:16:48.922: W/System.err(27155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-25 00:16:48.922: W/System.err(27155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
12-25 00:16:48.922: W/System.err(27155): at dalvik.system.NativeStart.main(Native Method)