我在浏览器上运行URL时得到JSONResponse是正确的,但是我运行了我的应用程序,然后 jsonObject是Get null ,这是我的异步类中的System.out.println。
实际上我在编码方面的错误是什么? Plase建议我。因为我得到了正确的JSON响应。
我在我的Async类中使用了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"
}
]
}
谢谢, 里纳
答案 0 :(得分:0)
也许您的系统会使用HttpGet请求,因此请将JSONfunctions
类代码更改为
表格
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
TO
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI website = new URI(url);
request.setURI(website);
HttpResponse response = httpclient.execute(request);
可能对你有帮助。