在滑动菜单的片段中加载带有齐射的json

时间:2015-11-05 08:10:31

标签: android json fragment

我正在尝试在滑动菜单的片段中加载json列表。 我只是试图将活动代码移动到片段。但它不起作用。 在活动中,一切都是正确的,它的确有效。 实际上我对片段并不专业。请告诉我什么是片段。

这就是我做的,请告诉我我犯了什么错误:

package info.androidhive.slidingmenu;

import info.androidhive.volleyjson.app.AppController;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.JsonObjectRequest;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

    public class HomeFragment extends Fragment {

    public static ListView gridView_url;
    ListViewAdapter_URL adapter;
    JSONArray jsonarray;

    // json object response url
    private String urlJsonObj = "http://www.musiclistening1.blogsky.com/";

    private static String TAG = MainActivity.class.getSimpleName();

    private ProgressDialog pDialog;

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        gridView_url = (ListView) rootView.findViewById(R.id.gridview_url);

        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
// Loading.............................................
        makeJsonObjectRequest();

        return rootView;
    }

    private void makeJsonObjectRequest() {

        showpDialog();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
                urlJsonObj, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());

                            try {
                                jsonarray = response.getJSONArray("music1");
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


                        adapter = new ListViewAdapter_URL(getActivity(), jsonarray);

                        gridView_url.setAdapter(adapter);


                        hidepDialog();
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        Toast.makeText(getActivity(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();
                        // hide the progress dialog
                        hidepDialog();
                    }
                });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }

    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

0 个答案:

没有答案