片段中的JSON ListVIew - doInBackground()错误

时间:2014-06-24 12:27:03

标签: android json listview android-fragments

我正在尝试通过点击按钮获取片段中的JSON列表。就在我以为我已经弄明白的时候,我撞到了墙上。所以,如果有人可以看看,如果你知道什么是错的,请向我解释,因为我是android初学者(仅限php和js)。

public class ExampleFragment extends Fragment {

    ListView list;
    TextView id;
    TextView zip;
    TextView city;
    TextView state;
    TextView country;
    Button Btngetdata;

    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

    //URL to get JSON Array
    private static String url = "http://url.com/web_services/get_cities.php";

    //JSON Node Names
    private static final String TAG_ARRAY = "cities";
    private static final String TAG_ID = "id";
    private static final String TAG_CITY = "city_text";
    private static final String TAG_ZIP = "zipcode_text";
    private static final String TAG_STATE = "state_text";
    private static final String TAG_COUNTRY = "country_text";

    JSONArray cities = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        oslist = new ArrayList<HashMap<String, String>>();
        Btngetdata = (Button)getView().findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                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();
            city = (TextView)getView().findViewById(R.id.city);
            state = (TextView)getView().findViewById(R.id.state);
            country = (TextView)getView().findViewById(R.id.country);
            id = (TextView)getView().findViewById(R.id.id);
            zip = (TextView)getView().findViewById(R.id.zip);

            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();


        }

        @Override
        protected JSONObject doInBackground(String... args) {

            // Getting JSON from URL
            json = jParser.getJSONFromUrl(url);

            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {

            pDialog.dismiss();
            try {
                 // Getting JSON Array from URL
                 cities = json.getJSONArray(TAG_ARRAY);

                 for(int i = 0; i < cities.length(); i++){

                     JSONObject c = cities.getJSONObject(i);

                     // Storing  JSON item in a Variable
                     String id = c.getString(TAG_ID);
                     String zip = c.getString(TAG_ZIP);
                     String city = c.getString(TAG_CITY);
                     String state = c.getString(TAG_STATE);
                     String country = c.getString(TAG_COUNTRY);

                     // Adding value HashMap key => value
                     HashMap<String, String> map = new HashMap<String, String>();
                     map.put(TAG_ID, id);
                     map.put(TAG_ZIP, zip);
                     map.put(TAG_CITY, city);
                     map.put(TAG_STATE, state);
                     map.put(TAG_COUNTRY, country);

                     oslist.add(map);

                     list = (ListView)getView().findViewById(R.id.list);

                     ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                         R.layout.list_item,
                         new String[] { TAG_CITY, TAG_STATE, TAG_COUNTRY, TAG_ID, TAG_ZIP }, new int[] {
                             R.id.city, R.id.state, R.id.country, R.id.id, R.id.zip});
                     list.setAdapter(adapter);
                     list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                         @Override
                         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                             Toast.makeText(getActivity(), "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
                         }
                     });
                 }
            } catch (JSONException e) {
              e.printStackTrace();
            }
        }
    }
}

编辑:Logcat发布 enter image description here

4 个答案:

答案 0 :(得分:2)

<manifest xlmns:android...>
     <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

答案 1 :(得分:2)

LogCat中的一切都很清楚:您忘记添加访问Internet的权限。将其添加到Manifest

<uses-permission android:name="android.permission.INTERNET"/>

答案 2 :(得分:2)

您的logcat明确说明Permission Denied (Missing Internet Permission),您需要在Manifest.xml文件中添加以下行

<uses-permission android:name="android.permission.INTERNET"/>

答案 3 :(得分:0)

为了将来参考,请添加以下代码,以避免始终为不同的应用程序添加Internet权限:

int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.CAMERA};

if(!hasPermissions(this,PERMISSIONS))
{
    ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}



public static boolean hasPermissions(Context context, String... permissions)
    {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null)
        {
            for (String permission : permissions)
            {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED)
                {
                    return false;
                }
            }
        }
        return true;
    }