从 URL 获取 JSON 字符串时出现 HTTP 连接问题

Http Connection Issues while fetching JSON string from URL

您好,我正在开发一个可以从 this url

获取 JSON 的应用程序

我是 HttpConnection 的新手,在 Android 中进行解析。

实际上我正在使用单独的 class 调用 JSONParser.java 来获取 JSON 和 return 它作为一个字符串到另一个 class 调用 SecondFragment.java(尝试使用 ViewPager 中的回收列表视图列出)

我无法从 URL 中获取 Json 字符串。 IE。问题出在 JSON 解析器中,它不是从 URL 到 return 中获取字符串。

下面是 JSONParser.java

的代码
public class JSONParser {

   // static InputStream is = null;
   // static JSONObject jObj = null;
    static String json = "";
String TAG="JSON Parser kbt";
    // constructor
    public JSONParser() {

    }

    public String getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            Log.d(TAG,"STAET GETJSONFROMURL");
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            Log.d(TAG,"STAET AFTR HTTP POST");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            Log.d(TAG,"STAET Aftr HTTP RESPONSE");
            HttpEntity httpEntity = httpResponse.getEntity();
            Log.d(TAG,"STAET AFTR HTTP ENTITY");
            is = httpEntity.getContent();
            Log.d(TAG,"END GETJSONFROMURL");
        } catch (UnsupportedEncodingException e) {Log.d(TAG,"Unsupported Encoding");
            e.printStackTrace();
        } catch (ClientProtocolException e) {Log.d(TAG,"ClientProtocolEx");
            e.printStackTrace();
        } catch (IOException e) {Log.d(TAG,"IOEX");
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }   

        // return JSON String
        Log.d(TAG,"RETURN JSON AS A STRING");
        return json;  // String is not returning

    }
}

在这一行之后,

HttpResponse httpResponse = httpClient.execute(httpPost);

Log.d 未打印。

我也会 post SecondFragment.java class 我也在调用 JSONParser class .

在 doInBackground() 中调用 JSONParser 的方法。

public class SecondFragment extends Fragment {

    ArrayList<FeedItem> feedItemList;
    private static final String TAG = "SecondFragment kbt";
    private SimpleRecyclerAdapter adapter;
    RecyclerView recyclerView;String parsedJson;
     String urls = "https://api.myjson.com/bins/1vx7x";
    public SecondFragment() {

    }

   // @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d(TAG, "Start second fragment");

        View view = inflater.inflate(R.layout.secondfragment, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.jsonrecyclerview);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        feedItemList = new ArrayList<>();

        new AsyncHttpTask().execute(urls);
        Log.d(TAG, "Last second fragment");

        return view;
    }



    public class AsyncHttpTask extends AsyncTask<String, String, Integer> {

        BufferedReader reader=null;

        @Override
        protected void onPreExecute() {
            //   setProgressBarIndeterminateVisibility(true);
            Log.d(TAG, "OnPreExecute");
        }

        @Override
        protected Integer doInBackground(String... params) {
Log.d(TAG,"DO IN BG START");
            Integer result = 0;
            JSONParser jobj=new JSONParser();
            parsedJson=jobj.getJSONFromUrl("http://api.myjson.com/bins/1vx7x");
            Log.d(TAG,"parsedJson string i s "+parsedJson);

             parseResult(parsedJson);
             result=1;            Log.d(TAG,"RETURN DO IN BG");
            return result;
        }

        @Override
        protected void onPostExecute(Integer result) {
            //setProgressBarIndeterminateVisibility(false);
            /* Download complete. Lets update UI */
         /* Download complete. Lets update UI */
            if (result == 1) {
                Log.i(TAG,"ON post exe");
                adapter = new SimpleRecyclerAdapter(getActivity(), feedItemList);
                recyclerView.setAdapter(adapter);
            } else {
                Log.e(TAG, "Failed to fetch data!");
            }


        }
        private void parseResult(String result) {
            try {
                JSONObject response = new JSONObject(result);
                JSONArray posts = response.optJSONArray("posts");

            /*Initialize array if null*/
                if (null == feedItemList) {
                    feedItemList = new ArrayList<FeedItem>();
                }

                for (int i = 0; i < posts.length(); i++) {
                    JSONObject post = posts.optJSONObject(i);

                    FeedItem item = new FeedItem();
                    item.setTitle(post.optString("title"));
                    item.setThumbnail(post.optString("thumbnail"));
                    feedItemList.add(item);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }



}

感谢您花时间阅读我的问题,并提前感谢您提供宝贵的答案:)

这是我的 SimpleRecyclerAdapter Class

public class SimpleRecyclerAdapter extends RecyclerView.Adapter<SimpleRecyclerAdapter.MainViewHolder> {
    List<String> versionModels;
    private ArrayList<FeedItem> feedItemList = new ArrayList<FeedItem>();
    private Context mContext;
    Boolean isHomeList = false;
    Boolean isJson=false;
    RecyclerView rv;
    private static final int TYPE_SIMPLELIST = 0;
    private static final int TYPE_JSONLIST = 1;

    public static List<String> homeActivitiesList = new ArrayList<String>();
    public static List<String> homeActivitiesSubList = new ArrayList<String>();
    Context context;
    OnItemClickListener clickListener;


    public void setHomeActivitiesList(Context context) {
        String[] listArray = context.getResources().getStringArray(R.array.home_activities);
        String[] subTitleArray = context.getResources().getStringArray(R.array.home_activities_subtitle);
        for (int i = 0; i < listArray.length; ++i) {
            homeActivitiesList.add(listArray[i]);
            homeActivitiesSubList.add(subTitleArray[i]);
        }
    }
    public SimpleRecyclerAdapter(Context context) {
        isHomeList = true;  Log.d("kbt","Constructor SIMPLE");
        this.context = context;
        setHomeActivitiesList(context);

    }
//   FOR JSON
    public SimpleRecyclerAdapter(Context context, ArrayList<FeedItem> feedItemList) {
        Log.d("kbt","Constructor JSON");
        isJson=true;
        this.feedItemList = feedItemList;
        this.mContext = context;
    }

    public SimpleRecyclerAdapter(List<String> versionModels) {
        isHomeList = false;
        this.versionModels = versionModels;

    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_SIMPLELIST : TYPE_JSONLIST;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        Log.d("kbt","Adapter Start OnCreate");
        switch(i){
            case TYPE_SIMPLELIST:Log.d("kbt","ADAP STAR ONCR first switch");
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlist_item, viewGroup, false);
                VersionViewHolder viewHolder = new VersionViewHolder(view);
                return viewHolder;

            case TYPE_JSONLIST:Log.d("kbt","ADAP STAR ONCR second switch");
                View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlist2_item, viewGroup, false);
                Log.d("kbt","ADAP STAR ONCR second switch 2nd line");
                JSONHolder viewHolder2 = new JSONHolder(view2);
                Log.d("kbt","ADAP STAR ONCR second switch 3nd line");
                return viewHolder2;


        }
        return null;

    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int i) {

        switch (holder.getItemViewType()){
            case TYPE_SIMPLELIST:Log.d("kbt","ONBIND First switch start");
                VersionViewHolder versionViewHolder=(VersionViewHolder)holder;
                if (isHomeList) {
                    versionViewHolder.title.setText(homeActivitiesList.get(i));
                    versionViewHolder.subTitle.setText(homeActivitiesSubList.get(i));
                } else {
                    versionViewHolder.title.setText(versionModels.get(i));
                }Log.d("kbt","ONBIND First switch end");
                break;
            case TYPE_JSONLIST:Log.d("kbt","ONBIND second switch start");
                JSONHolder jsonHolder=(JSONHolder)holder;
                Log.d("kbt","ONBIND second switch premiddle");
                FeedItem feedItem = feedItemList.get(i);
                Log.d("kbt","ONBIND second switch middle");
                Picasso.with(mContext).load(feedItem.getThumbnail())
                        .error(R.drawable.placeholder)
                        .placeholder(R.drawable.placeholder)
                        .into(jsonHolder.thumbnail);
                Log.d("kbt", "ONBIND second switch 2nd middle");
                jsonHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
                Log.d("kbt", "ONBIND second switch end");


        }

    }

    @Override
    public int getItemCount() {
        if(isJson){
            return (null != feedItemList ? feedItemList.size() : 0);
        }
        else{
            if (isHomeList)
                return homeActivitiesList == null ? 0 : homeActivitiesList.size();
            else
                return versionModels == null ? 0 : versionModels.size();
        }

    }


    class VersionViewHolder extends MainViewHolder implements View.OnClickListener {
        CardView cardItemLayout;
        TextView title;
        TextView subTitle;


        public VersionViewHolder(View itemView) {
            super(itemView);

            rv=(RecyclerView)itemView.findViewById(R.id.dummyfrag_scrollableview);

            cardItemLayout = (CardView) itemView.findViewById(R.id.cardlist_item);
            title = (TextView) itemView.findViewById(R.id.listitem_name);
            subTitle = (TextView) itemView.findViewById(R.id.listitem_subname);

            if (isHomeList) {
                itemView.setOnClickListener(this);
                Log.d("kbt","Inside first");
            } else {
                subTitle.setVisibility(View.GONE);
                /*View.GONE This view is invisible, and it doesn't take any space for layout purposes.
                  View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.*/
            }

        }

        @Override
        public void onClick(View v) {
            Log.d("kbt", "Inside second first");
           clickListener.onItemClick(v,getAdapterPosition());
            Log.d("kbt", "Inside second");

        }

    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);


    }

    public void SetOnItemClickListener(final OnItemClickListener itemClickListener) {
        this.clickListener = itemClickListener;


    }

    class JSONHolder extends MainViewHolder {

        protected ImageView thumbnail;
        protected TextView title;
        public JSONHolder(View itemView) {
            super(itemView);
            Log.d("kbt","JSON Inside HOLDER");
            rv=(RecyclerView)itemView.findViewById(R.id.jsonrecyclerview);
            thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
            title = (TextView) itemView.findViewById(R.id.title);


            }

        }


    public class MainViewHolder extends  RecyclerView.ViewHolder {
       public MainViewHolder(View v) {
        super(v);
       }
    }

    }

错误:

11-13 14:36:43.645  21502-21502/? I/Process﹕ Sending signal. PID: 21502 SIG: 9
11-13 14:36:43.645  21502-21502/? D/AndroidRuntime﹕ procName from cmdline: corp.trees.com.mdesign
11-13 14:36:43.645  21502-21502/? E/AndroidRuntime﹕ in writeCrashedAppName, pkgName :corp.trees.com.mdesign
11-13 14:36:43.645  21502-21502/? D/AndroidRuntime﹕ file written successfully with content: corp.trees.com.mdesign StringBuffer : ;corp.trees.com.mdesign
11-13 14:36:43.645  21502-21502/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: corp.trees.com.mdesign, PID: 21502
    java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.get(ArrayList.java:308)
            at corp.trees.com.mdesign.SimpleRecyclerAdapter.onBindViewHolder(SimpleRecyclerAdapter.java:108)
            at corp.trees.com.mdesign.SimpleRecyclerAdapter.onBindViewHolder(SimpleRecyclerAdapter.java:21)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5084)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4385)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4278)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1947)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1359)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1322)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:556)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2673)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2971)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1626)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1009)
            at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:719)
            at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
            at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:984)
            at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:732)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14852)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1994)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1007)
            at android.view.ViewRootImpl$T

我认为你最好使用 GET 方法而不是 POST,并尝试使用 HttpURLConnection,这是一个示例:

URL requestUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)requestUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();

我在你的适配器中看到很多奇怪的东西,如果我错了请纠正我但是

根据 documentation getItemCount() 必须 return 适配器持有的项目总数。在您的适配器中,您使用的是多个列表,并且 getItemCount() 是 return 的大小,具体取决于某些变量,但是 getItemCount() 只会被回收器视图调用一次,因此这仅在以下情况下有效每个 SimpleRecyclerAdapter 个实例您只使用其中一个列表。您只初始化 feedItemList,其他列表保持为空。所以我假设这就是你想要做的。

但是在获取当前元素类型时,您使用:

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_SIMPLELIST : TYPE_JSONLIST;
    }

这意味着您的第一个元素将是 TYPE_SIMPLELIST,其他元素将是 TYPE_JSONLIST。这对我来说没有意义,因为你的列表中只有一个被初始化 (feedItemList),所以我认为你应该 return 像这样:

    @Override
    public int getItemViewType(int position) {
        return isJson? TYPE_JSONLIST : TYPE_SIMPLELIST;
    }

所以它总是从feedItemList读取数据。