仅显示连接到服务器,而不是加载JSON数据

时间:2017-11-18 14:40:35

标签: android json httpurlconnection

我正在尝试使用HttpURLConnection获取一些JSON数据,但我只是连接服务器显示而没有任何反应。

这是我的代码:

@SuppressWarnings("deprecation")
public class JSONParsingFragment extends Fragment{
    ListView lv;
    ArrayList<Actors> actorsList;
    ActorAdapter adapter;

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

        View vw=inflater.inflate(R.layout.jsonparse_fragment, container, false);
        lv=(ListView) vw.findViewById(R.id.listView1);

        actorsList = new ArrayList<Actors>();
        new JSONAsynTask().execute("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors");
        adapter = new ActorAdapter(getActivity(), R.layout.jsonparsedata_item, actorsList);
        lv.setAdapter(adapter);
        return  vw;
    }

    class JSONAsynTask extends AsyncTask<String, Void, Void> {
        String result;
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(getActivity());
            dialog.setMessage("Loading, please wait");
            dialog.setTitle("Connecting server");
            dialog.show();
            dialog.setCancelable(false);
        }

        @Override
        protected Void doInBackground(String... strings) {
            URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(strings[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                int responseCode = urlConnection.getResponseCode();

                if(responseCode == HttpURLConnection.HTTP_OK){

                    JSONObject jsono = new JSONObject(String.valueOf(url));
                    JSONArray jarray = jsono.getJSONArray("actors");

                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject object = jarray.getJSONObject(i);

                        Actors actor = new Actors();

                        actor.setName(object.getString("name"));
                        actor.setDescription(object.getString("description"));
                        actor.setDob(object.getString("dob"));
                        actor.setCountry(object.getString("country"));
                        actor.setHeight(object.getString("height"));
                        actor.setSpouse(object.getString("spouse"));
                        actor.setChildren(object.getString("children"));
                        actor.setImage(object.getString("image"));

                        actorsList.add(actor);

                        Log.v("log", "response");
                    }


                } } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;}

        protected void onPostExecute(Boolean result) {

            dialog.dismiss();
            adapter.notifyDataSetChanged();
            if(result == false)
                Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

        }
    }

}

在控制台日志中,我有:

org.json.JSONException: Value http of type java.lang.String cannot be converted to JSONObject

所以我觉得这部分有错误:

 if(responseCode == HttpURLConnection.HTTP_OK){

                        JSONObject jsono = new JSONObject(String.valueOf(url));
                        JSONArray jarray = jsono.getJSONArray("actors");

有人可以帮帮我吗?

0 个答案:

没有答案