猿问

求解为什么Log.d("xyz", jsonString)的jsonString值显示为null

package com.example.news;


import java.io.BufferedReader;

import java.net.URL;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.net.MalformedURLException;

import java.util.ArrayList;

import java.util.List;


import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.util.Log;

import android.widget.ListView;


public class MainActivity extends Activity {


private static String URL = "http://www.imooc.com/api/teacher?type=4&num=30";

private ListView listView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.mainListView);

        

        new NewAsyncTask().execute(URL);

        

    }

    

    private List<NewsBean> getJsonDate(String url)

    {

    List<NewsBean> newBeanList = new ArrayList<NewsBean>();

    try {

String jsonString = readStream(new URL(url).openStream());

Log.d("xyz", jsonString);

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

   

return newBeanList;

   

    }

    

    private String readStream(InputStream is){

   

    InputStreamReader isr;

    String result = "";

    try {

    String line = "";

isr = new InputStreamReader(is,"utf-8");

BufferedReader br = new BufferedReader(isr);

while ((line = br.readLine())!=null);

{

result += line;

}

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

   

return result;

   

    }


    class NewAsyncTask extends AsyncTask<String, Void, List<NewsBean>>{


@Override

protected List<NewsBean> doInBackground(String... arg0) {

// TODO Auto-generated method stub

return getJsonDate(arg0[0]);

}

   

    }

}

qq_冲锋_1
浏览 1627回答 2
2回答

qq_冲锋_1

我知道问题出在那里了,是网址错。

justindo

Log.d("xyz", jsonString);在执行完String jsonString = readStream(new URL(url).openStream());这句后就马上执行了,还没有从url拿到结果呢。 
随时随地看视频慕课网APP

相关分类

Android
我要回答