首页 新闻 赞助 找找看

为什么在创建URL 对象后的代码都不执行了

0
悬赏园豆:50 [已关闭问题] 关闭于 2018-01-05 18:34
package com.example.fishionnews.newutils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

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

import android.content.Context;
import android.util.Log;

import com.example.fishionnews.R;
import com.example.fishionnews.Beans.NewsBean;

/**
 * 通过HttpURLconntion 对象来访问服务端,由此来获得listview 要显示的数据
 * */
public class listutils {
    
    
    // 3.封装一些假数据
    public static String newspath_url = "http://192.168.17.1:8080/itheima74/servlet/GetNewsServlet";

    public static ArrayList<NewsBean> getnewsForNetWork(Context context) throws Exception {    
        // TODO Auto-generated method stub
        ArrayList<NewsBean> arraylist = new ArrayList<NewsBean>();
        
        // 1.网络请求数据
        try {
            Log.e("news","------执行1-------");
            URL url = new URL("newspath_url");    
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //设置连接的方式和超时时间
            
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(10*1000);
        
            //获取请求响应码
            int code = connection.getResponseCode();
            
            Log.e("news", code+"");        
            if(code == 200){
                Log.e("news", "aaa");
                //获取请求到的流信息
                InputStream inputStream = connection.getInputStream();
                String result = StreamUtil.Streamtostring(inputStream);
                if(result==null){
                    Log.e("news", "null");        
                }else{
                    Log.e("news", result);    
                }
                
                //2.解析获取的新闻数据到List集合中。
                    JSONObject root_json = new JSONObject(result);//将一个字符串封装成一个json对象。
                    JSONArray jsonArray = root_json.getJSONArray("newss");//获取root_json中的newss作为jsonArray对象
                    
                    for (int i = 0 ;i < jsonArray.length();i++){//循环遍历jsonArray
                        JSONObject news_json = jsonArray.getJSONObject(i);//获取一条新闻的json
                        
                        NewsBean newsBean = new NewsBean();                    
                        newsBean.id = news_json.getInt("id");
                        newsBean.comment = news_json.getInt("comment");//评论数
                        newsBean.type = news_json.getInt("type");//新闻的类型,0 :头条 1 :娱乐 2.体育
                        newsBean.time = news_json.getString("time");
                        newsBean.des = news_json.getString("des");
                        newsBean.title = news_json.getString("title");
                        newsBean.news_url = news_json.getString("news_url");
                        newsBean.icon_url = news_json.getString("icon_url");
                        
                        arraylist.add(newsBean);
            
                    }
                // 3.将数据缓存到数据库中

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return arraylist;
    }

}
古叶丶扁舟的主页 古叶丶扁舟 | 初学一级 | 园豆:9
提问于:2017-06-03 19:11
< >
分享
所有回答(2)
0

没报错信息,没输出信息,你想说什么?

TCG2008 | 园豆:1150 (小虾三级) | 2017-06-03 23:32

我打印了log信息在创建URL对象后就不能打印出信息了,也就是说网络请求的代码没有执行为什么没有执行啊

在判断语句判断请求码中是否为200 为200就打印结果 不为就打印null 可是两个结果没有一个打印出来我不知道为什么不执行下面的代码

支持(0) 反对(0) 古叶丶扁舟 | 园豆:9 (初学一级) | 2017-06-03 23:44

@751638623: 

1.确认是否能够执行到 return arraylist;

2.确认是否 e.printStackTrace(); 有内容输出

支持(0) 反对(0) TCG2008 | 园豆:1150 (小虾三级) | 2017-06-04 00:01

@TCG2008: 只执行到URL url = new URL(url_path);

支持(0) 反对(0) 古叶丶扁舟 | 园豆:9 (初学一级) | 2017-06-04 00:14

@751638623: 

不可能没有输出日志的,你好好看一下verbose级别的logcat日志,假如你的代码是这么写的 URL url = new URL("newspath_url");  本身就有 java.net.MalformedURLException 了,printStackTrace肯定是能打印出来的, 如果真的什么东西都没有,那只能祈求奇迹了。

支持(0) 反对(0) TCG2008 | 园豆:1150 (小虾三级) | 2017-06-04 11:17
0

看看有没有异常信息, 或者返回的不是200

苍枫露雨 | 园豆:1027 (小虾三级) | 2017-07-10 16:17

可以打个断点调试看下,

支持(0) 反对(0) 苍枫露雨 | 园豆:1027 (小虾三级) | 2017-07-10 16:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册