【问题标题】:get jsonResponse is null in android?在android中获取jsonResponse为null?
【发布时间】:2015-05-16 12:01:43
【问题描述】:

在浏览器上运行 URL 时,我得到了正确的 JSONResponse,但我已经运行了我的应用程序,然后 jsonObject 是 Get null,这是我的异步类中的 System.out.println。

实际上我在编码方面的错误是什么?请建议我。因为我得到了正确的 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"
}
]
}

谢谢, 雷纳

【问题讨论】:

    标签: android jsonresponse


    【解决方案1】:

    也许你的系统接受了 HttpGet Request ,所以将JSONfunctions类代码改为

    表格

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
    

    HttpClient httpclient = new DefaultHttpClient();
    
               HttpGet request = new HttpGet();
               URI website = new URI(url);
               request.setURI(website);
               HttpResponse response = httpclient.execute(request);
    

    也许对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 2015-04-30
      • 1970-01-01
      相关资源
      最近更新 更多