java.net.UnknownHostException:无法解析主机" gist.githubusercontent.com":没有与主机名关联的地址

时间:2017-06-07 12:19:23

标签: android json

我写了json并在git的帮助下创建了url。但是现在我无法获得所请求的数据,因为结果即将到来null。请帮助我

public class TestActivity extends AppCompatActivity {
    Button btnHit;
   ListView moviList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
         btnHit= (Button) findViewById(R.id.btn);
        moviList=(ListView)findViewById(R.id.lvMovie);

       /* btnHit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new JSONTask().execute("https://gist.githubusercontent.com/PoonamWadekar/5c69afdcbe9c68240c546f73bcb40c69/raw/050254dd568a972442cdff0d984c396b9b340b7f/movie.json");
            }

        });*/

    }

    public class JSONTask extends AsyncTask<String,String,List<MovieModel>>{

        @Override
        protected List<MovieModel> doInBackground(String... params) {
            HttpURLConnection httpURLConnection = null;
            BufferedReader reader = null;
            try {
                URL url = new URL(params[0]);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.connect();
                InputStream inputStream = httpURLConnection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                String finalJson=buffer.toString();
                JSONObject parentObj=new JSONObject(finalJson);
                JSONArray parentArray=parentObj.getJSONArray("movies");
                List<MovieModel> movieModelList=new ArrayList<>();

                for (int i=0; i<parentArray.length() ; i++)
                {
                JSONObject finalObj=parentArray.getJSONObject(i);
                    MovieModel movieModel=new MovieModel();
                    movieModel.setMovie(finalObj.getString("movie"));
                    movieModel.setYear(finalObj.getInt("year"));
                    movieModel.setRating((float) finalObj.getDouble("rating")/2);
                    movieModel.setStory(finalObj.getString("story"));
                    //movieModel.setImage(finalObj.getString("image"));
                    List<MovieModel.Caste> casteList=new ArrayList<>();
                    for (int j=0;j<finalObj.getJSONArray("caste").length();j++){
                        MovieModel.Caste caste=new MovieModel.Caste();
                        caste.setName(finalObj.getJSONArray("caste").getJSONObject(j).getString("name"));
                        casteList.add(caste);
                    }
                    movieModel.setCasteList(casteList);

                    movieModelList.add(movieModel);
                    Log.d("hi","list+++++++++++++++++++++++" +movieModelList);
                }
                return movieModelList;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (httpURLConnection != null)
                    httpURLConnection.disconnect();
                try {
                    if (reader != null)
                        reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;

        }
        @Override
        protected void onPostExecute(List<MovieModel> result) {
            super.onPostExecute(result);
            if (result==null){
                Log.d("Hi","sorry no result");
                return;
            }

            //set data to recycler view
            MovieAdapter adapter=new MovieAdapter(getApplicationContext(),R.layout.movie_cell,result);

            moviList.setAdapter(adapter);


        }

    }


    public class MovieAdapter extends ArrayAdapter{
        private List<MovieModel> movieModelList=new ArrayList<MovieModel>();
        private int resource;
        LayoutInflater inflater;

        public MovieAdapter(Context context, int resource, List<MovieModel> objects) {
            super(context, resource, objects);
            movieModelList=objects;
            this.resource=resource;
            inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);


        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null)
            {
                convertView=inflater.inflate(resource,null);
            }
            ImageView movieIcon;
            TextView name;
            TextView year;
            RatingBar ratingBar;
            TextView tvCaste;
            TextView story;


            movieIcon= (ImageView) convertView.findViewById(R.id.place_image);
            name= (TextView) convertView.findViewById(R.id.tv_name);
            year= (TextView) convertView.findViewById(R.id.tv_year);
            ratingBar= (RatingBar) convertView.findViewById(R.id.rt_ratebar);
            tvCaste= (TextView) convertView.findViewById(R.id.tv_cast);
            story= (TextView) convertView.findViewById(R.id.tv_story);


            name.setText(movieModelList.get(position).getMovie());
            year.setText(" year -"+ movieModelList.get(position).getYear());
            StringBuffer stringBuffer=new StringBuffer();
            for (MovieModel.Caste caste: movieModelList.get(position).getCasteList()){
                stringBuffer.append(caste.getName() + ",");
            }
            tvCaste.setText(stringBuffer);

            story.setText(movieModelList.get(position).getStory());
            // rating bar
            ratingBar.setRating(movieModelList.get(position).getRating()/2);

            return convertView;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.actionbar_profile,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id=item.getItemId();

        if(id==R.id.refresh){
            String url="https://gist.githubusercontent.com/PoonamWadekar/5c69afdcbe9c68240c546f73bcb40c69/raw/9e3de75ffa48c70f81ad07b7623e5fd0789142f0/movie.json";
           Log.d("url____",url);
            new JSONTask().execute(url);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

我的设备运行正常。 对你的情况几乎没有疑问。

  • 您是否在manifest.xml文件中设置了互联网权限
  • 您是否正在使用模拟器运行应用程序?
  • 如果您使用的是WiFi,请尝试重新连接并再次检查。
  • 你在使用VPN吗?如果是,则断开与VPN的连接,然后重试。 感谢