在浏览器上运行 URL 时,我得到了正确的 JSONResponse,但我已经运行了我的应用程序,然后jsonObject 为 Get null,即我的异步类中的 System.out.println。
实际上我在编码方面的错误是什么?请建议我。因为我得到了正确的 JSON 响应。
我为我的异步类使用 JSONFunctions 类,URL 是http://www.androidbegin.com/tutorial/android-json-parse-images-and-texts-tutorial/
我的 JAVA 代码是,
public class CategoryFragment extends Fragment {
JSONObject jsonobject = null;
JSONArray jsonarray = null;
ProgressDialog progressDialog = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initializeWidgets(rootView);
new GetData().execute();
}
return rootView;
}
public class GetData extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Please Wait...");
progressDialog.setCancelable(false);
progressDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
// Retrieve JSON Objects from the given URL address
jsonobject = JSONFunctions
.getJSONfromURL("http://example.com/test/get_category.php?id=16");
System.out.println("!!!!!!!!!jsonobject===="
+ jsonobject);
if (jsonobject != null) {
try {
// Locate the array name in JSON
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> hashMap = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
hashMap.put("status", jsonobject.getString("status"));
hashMap.put("ID", jsonobject.getString("ID"));
hashMap.put("category",
jsonobject.getString("category"));
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
} else {
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
/** Initialize Widgets */
private void initializeWidgets(View rootView) {
/** TextView */
txtTitle = (TextView) rootView.findViewById(R.id.txtTitle);
}
}
我的 JSON 响应是,
{
data: [
{
status: 1,
ID: "40",
category: "FIRST"
},
{
status: 1,
ID: "41",
category: "SECOND"
}
]
}
谢谢,丽娜