使用HttpUrlConnection的Android异步任务

时间:2017-09-18 08:59:02

标签: android

我一直在尝试使用httpurlconnection调用异步任务。我希望将Json数据输入到正在创建自定义列表视图的适配器中。但我的应用程序崩溃了。我已经尝试了很多不同的代码,这些代码已经在这个堆栈溢出中讨论过,但我只是想不通。我不确定这是不正确的代码还是我的逻辑。在Android开发方面我是业余爱好者,所以我非常感谢帮助。

这是声明异步函数的类,它是片段的一部分。

public class Movies extends Fragment {

ListView List;
ListAdapter ladapter;
ArrayList<info> data;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_movies, container, false);
    movielist mv=new movielist();
    Toast.makeText(getContext(), "calling", Toast.LENGTH_LONG).show();
    mv.execute("myurl");
    return view;
}

public class movielist extends AsyncTask<String, Void, String> {
    HttpURLConnection urlConnection;
    @Override
    protected String doInBackground(String... args) {

        String result = null;

        try {
             URL myurl=new URL(args[0]);
             HttpURLConnection urlConnection = (HttpURLConnection) myurl
                    .openConnection();
              urlConnection.setRequestMethod("GET");
              urlConnection.setDoInput(true);
              urlConnection.connect();
              InputStream is=urlConnection.getInputStream();
              if (is != null) {
                StringBuilder sb = new StringBuilder();
                String line;
                try {
                     BufferedReader reader = new BufferedReader(
                     new InputStreamReader(is));
                     while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                    reader.close();
                } finally {
                    is.close();
                }
                result = sb.toString();
            }
        }catch (Exception e){
            result=null;
        }
        return result;
    }


    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getContext(), "In post ex", Toast.LENGTH_SHORT).show();
            try {
                    String data1 = result;
                    JSONObject jobj = new JSONObject(data1);
                    JSONArray jArray = jobj.getJSONArray("actors");
                    for (int i = 0; i < jArray.length(); i++) {
                    info information = new info();
                    JSONObject jrealobj = jArray.getJSONObject(i);
                    information.setTitle(jrealobj.getString("name"));
                    information.setPoster_path(jrealobj.getString("image"));
                    data.add(information);
                }
            } catch (JSONException e) {Toast.makeText(getContext(), "Not working", Toast.LENGTH_SHORT).show();
            }
            try {
                 ladapter = new Listadapter(getContext(), data);
                 List.setAdapter(ladapter);}catch (NullPointerException e){Toast.makeText(getContext(), "Null", Toast.LENGTH_SHORT).show();}
              }
}

}

1 个答案:

答案 0 :(得分:0)

在清单文件中添加互联网权限并检查。