无法加载api请求内容

时间:2018-01-28 08:16:36

标签: java android

我正在尝试从api将文本加载到textView中。但是,当我在应用程序上运行它时,它进入异常处理程序并返回错误。 为什么不起作用? 附:新手

公共类MainActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView1= (TextView) findViewById(R.id.textView1);
    String ans=new Down().doInBackground();
    textView1.setText(ans);
}

private class Down extends AsyncTask< String, Void, String>{
    @Override
    protected String doInBackground(String... urls){
        String result="No";
        String disc="https://api.themoviedb.org/3/discover/movie?api_key="+api_key+"&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1";
        try{
            URL url=new URL(disc);
            HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();

            try{
                BufferedReader br=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder stb= new StringBuilder();

                String line;
                while((line=br.readLine())!=null){
                    stb.append(line).append("\n");
                }
                br.close();
                result= stb.toString();
            }
            finally {
                urlConnection.disconnect();
            }
        }
        catch (Exception e){
            Log.e("ERROR", e.getMessage(),e);
        }

        if(result==null)
            result="There was an error";

        Log.i("INFO",result);
        return result;
    }
}

}

1 个答案:

答案 0 :(得分:0)

您没有正确使用AsyncTask并且您尝试使用https://打开HttpURLConnection网址,您应该使用HttpsURLConnection代替。

试试这个:

public class MainActivity extends AppCompatActivity {

    TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1 = (TextView) findViewById(R.id.textView1);
        new Down().execute();
    }

    private class Down extends AsyncTask<Void, Void, String>{
        @Override
        protected String doInBackground(Void... v){
            String result="No";
            String disc="https://api.themoviedb.org/3/discover/movie?api_key="+api_key+"&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1";
            try{
                URL url=new URL(disc);
                HttpsURLConnection urlConnection=(HttpsURLConnection) url.openConnection();

                try{
                    BufferedReader br=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    StringBuilder stb= new StringBuilder();

                    String line;
                    while((line=br.readLine())!=null){
                        stb.append(line).append("\n");
                    }
                    br.close();
                    result= stb.toString();
                }
                finally {
                    urlConnection.disconnect();
                }
            }
            catch (Exception e){
                Log.e("ERROR", e.getMessage(),e);
            }

            if(result==null)
                result="There was an error";

            Log.i("INFO",result);
            return result;
        }
    }
    @Override
    protected void onPreExecute(String result) {
        textView1.setText(result);
    }
}