从URL获取JSON字符串时的Http连接问题

时间:2015-11-13 08:12:07

标签: android json httpresponse

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

获取JSON的应用

我是新手HttpConnection并在Android中解析。

实际上我使用名为JSONParser.java的单独类来获取JSON并将其作为String返回到另一个名为SecondFragment.java的类(尝试使用ViewPager中的回收列表视图列出)

我无法从URL获取Json字符串。 即。问题出在JSONParser中,它不是从URL中提取字符串来返回。

以下是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未打印。

我也会在我调用JSONParser类的地方发布SecondFragment.java类。

在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类

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

根据documentation getItemCount()必须返回适配器所拥有的项目总数。在您的适配器中,您使用多个列表,并且getItemCount()根据某些变量返回一个大小,但getItemCount()将仅由回收器视图调用一次,因此这仅在您只使用其中一个时才会起作用每个SimpleRecyclerAdapter实例的那些列表。您只是初始化feedItemList,其他列表保持为空或空。所以我会假设这是你要做的事情。

但是在获取当前的elementType时使用:

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

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

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

因此它始终从feedItemList读取数据。