我想从片段类中获取mysql db的配置文件详细信息。应显示到textview。比如学生姓名,地址,手机等。我用JSON格式从php发送数据。
这是我的代码
public class ProfileFragment extends Fragment {
private View parentView;
// Session Manager Class
SessionManager session;
TextView student_name;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private String url;
//JSON Node Names
private static final String TAG_ARRAY = "student_profile";
private static final String TAG_NAME = "student_name";
JSONArray student_profile = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return inflater.inflate(R.layout.profile, container, false);
parentView = inflater.inflate(R.layout.profile, container, false);
//session.checkLogin();
session = new SessionManager(getActivity());
session.checkLogin();
return parentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
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;
@Override
protected void onPreExecute() {
super.onPreExecute();
student_name = (TextView)getView().findViewById(R.id.studentname);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
// get user data from session
HashMap<String, String> user = session.getUserDetails();
// name
String emailaddr = user.get(SessionManager.KEY_NAME);
url = "http://XXXXXXX.org/api/profile.php?emailid="+emailaddr;
// Getting JSON from URL
json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
student_profile = json.getJSONArray(TAG_ARRAY);
for(int i = 0; i < student_profile.length(); i++){
JSONObject c = student_profile.getJSONObject(i);
String student_firstname = null;
// Storing JSON item in a Variable
String name = c.getString(student_firstname);
Toast.makeText(getActivity(), "name="+name, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
我收到了这样的错误
12-24 09:29:44.817: W/System.err(30999): org.json.JSONException: No value for student_profile
12-24 09:29:44.818:W / System.err(30999):at org.json.JSONObject.get(JSONObject.java:355) 12-24 09:29:44.819:W / System.err(30999):at org.json.JSONObject.getJSONArray(JSONObject.java:549) 12-24 09:29:44.819:W / System.err(30999):at com.tutorialsface.ResideMenuDemo.ProfileFragment $ JSONParse.onPostExecute(ProfileFragment.java:89)
答案 0 :(得分:1)
我认为你的json没有TAG_ARRAY的值。所以它给出了JSONException。 您应首先检查以下值:
if (json.has(TAG_ARRAY)) {
student_profile = json.getJSONArray(TAG_ARRAY);
}
然后它不会产生JSONException。