Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AsyncTask未使用BaseApter创建GridView_Java_Android_Gridview_Android Asynctask_Baseadapter - Fatal编程技术网

Java AsyncTask未使用BaseApter创建GridView

Java AsyncTask未使用BaseApter创建GridView,java,android,gridview,android-asynctask,baseadapter,Java,Android,Gridview,Android Asynctask,Baseadapter,我是android开发新手,对此我有非常基本的了解。到目前为止,我所取得的任何成就都是通过这个网站或youtube视频实现的。我被困在AsyncTask中(之前我在Create View上使用了.get(),它工作得很好,但UI被阻止,直到任务完成。为了避免UI被阻止,我被建议删除.get())从OnCreateView()函数删除此项后,现在无法从AsyncTask获取任何数据)。我这样做了,但现在我无法创建视图。我做了很多研究,但无法获得这种优势 这是我的代码,请帮助如何从中创建视图 OnC

我是android开发新手,对此我有非常基本的了解。到目前为止,我所取得的任何成就都是通过这个网站或youtube视频实现的。我被困在AsyncTask中(之前我在Create View上使用了.get(),它工作得很好,但UI被阻止,直到任务完成。为了避免UI被阻止,我被建议删除.get())从OnCreateView()函数删除此项后,现在无法从AsyncTask获取任何数据)。我这样做了,但现在我无法创建视图。我做了很多研究,但无法获得这种优势

这是我的代码,请帮助如何从中创建视图

OnCreateView():-

异步任务代码:-

    public class JSONTask extends AsyncTask<String,String,String> {
    private ProgressDialog mProgressDialog;

    int progress;
    public JSONTask(){
        mProgressDialog = new ProgressDialog(getContext());
        mProgressDialog.setMax(100);
        mProgressDialog.setProgress(0);
    }

    @Override
    protected void onPreExecute(){
        mProgressDialog = ProgressDialog.show(getContext(),"Loading","Loading Data...",true,false);
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        final String finalJson = params[1];
        String json = finalJson;
        try{
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(5000);
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            connection.setRequestProperty("A-APK-API", "******");
            connection.setRequestProperty("Authorization", "Basic **:**");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.connect();

            OutputStream stream = connection.getOutputStream();
            OutputStreamWriter streams =  new OutputStreamWriter(stream, "UTF-8");
            stream.write(json.getBytes("UTF-8"));
            stream.close();

            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));

            StringBuffer buffer = new StringBuffer();

            String line = "";
            while((line = reader.readLine()) != null){
                buffer.append(line);
            }
            return buffer.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(connection != null){
                connection.disconnect();
            }
            try {
                if(reader != null) {
                    reader.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }
        return null;
    }
    protected void onPostExecute(String result){
        super.onPostExecute(result);
        Json_String = result;
        Toast.makeText(getContext(),result,Toast.LENGTH_LONG).show();
        mProgressDialog.dismiss();
    }
}
公共类JSONTask扩展异步任务{
private ProgressDialog mProgressDialog;
智力进步;
公共JSONTask(){
mProgressDialog=newprogressdialog(getContext());
mProgressDialog.setMax(100);
mProgressDialog.setProgress(0);
}
@凌驾
受保护的void onPreExecute(){
mProgressDialog=ProgressDialog.show(getContext(),“加载”,“加载数据…”,true,false);
super.onPreExecute();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpURLConnection=null;
BufferedReader reader=null;
最终字符串finalJson=params[1];
字符串json=finalJson;
试一试{
URL=新URL(参数[0]);
connection=(HttpURLConnection)url.openConnection();
连接。设置连接超时(5000);
setRequestProperty(“内容类型”,“应用程序/json;字符集=UTF-8”);
connection.setRequestProperty(“A-APK-API”,“*******”);
connection.setRequestProperty(“授权”、“基本**:*”);
connection.setDoOutput(真);
connection.setDoInput(true);
connection.setRequestMethod(“POST”);
connection.connect();
OutputStream=connection.getOutputStream();
OutputStreamWriter流=新的OutputStreamWriter(流,“UTF-8”);
stream.write(json.getBytes(“UTF-8”);
stream.close();
reader=新的BufferedReader(新的InputStreamReader(connection.getInputStream(),“UTF-8”);
StringBuffer=新的StringBuffer();
字符串行=”;
而((line=reader.readLine())!=null){
buffer.append(行);
}
返回buffer.toString();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
if(连接!=null){
连接断开();
}
试一试{
if(读卡器!=null){
reader.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
Json_String=结果;
Toast.makeText(getContext(),result,Toast.LENGTH_LONG).show();
mProgressDialog.disclose();
}
}

请在此帮助我

您应该更改创建适配器和连接的方式 你应该这样做

1.首先通过
AsyncTask
doInBackGround
方法获取列表、数组列表等中的数据

  • 然后在
    onPostExecute
    方法上检索数据并创建适配器并将其附加到视图
  • 在获取数据时,可以显示一些
    进度对话框
  • 如果您的
    AsyncTask
    在另一个单独的类中,则使用接口从您的
    AsyncTask
    类中获取数据 看看这个

  • 如果不使用.get(),则无法从asynctask获得结果

    所以,改变这种说法。只启动异步任务

    然后将所有代码放在AsyncTask的onPostExecute()中该行之后

    就这些

        class CustomAdpter extends BaseAdapter {
    
        String[] type;
        Integer[] count;
        public CustomAdpter(DashboardFragment dashboardFragment, String[] type, Integer[] count){
            this.count = count;
            this.type = type;
        }
        @Override
        public int getCount() {
            return type.length;
        }
    
        @Override
        public Object getItem(int i) {
            return null;
        }
    
        @Override
        public long getItemId(int i) {
            return 0;
        }
    
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getLayoutInflater().inflate(R.layout.grid_single_itme,null);
            TextView textView = (TextView) view.findViewById(R.id.TextView1);
            TextView textView1 = (TextView) view.findViewById(R.id.textView2);
            textView.setText(String.valueOf(count[i]));
            textView1.setText(type[i]);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getActivity(),"Booking Item Clicked",Toast.LENGTH_LONG).show();
                }
            });
            return view;
        }
    }
    
        public class JSONTask extends AsyncTask<String,String,String> {
        private ProgressDialog mProgressDialog;
    
        int progress;
        public JSONTask(){
            mProgressDialog = new ProgressDialog(getContext());
            mProgressDialog.setMax(100);
            mProgressDialog.setProgress(0);
        }
    
        @Override
        protected void onPreExecute(){
            mProgressDialog = ProgressDialog.show(getContext(),"Loading","Loading Data...",true,false);
            super.onPreExecute();
        }
    
        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;
            final String finalJson = params[1];
            String json = finalJson;
            try{
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(5000);
                connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                connection.setRequestProperty("A-APK-API", "******");
                connection.setRequestProperty("Authorization", "Basic **:**");
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST");
                connection.connect();
    
                OutputStream stream = connection.getOutputStream();
                OutputStreamWriter streams =  new OutputStreamWriter(stream, "UTF-8");
                stream.write(json.getBytes("UTF-8"));
                stream.close();
    
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
    
                StringBuffer buffer = new StringBuffer();
    
                String line = "";
                while((line = reader.readLine()) != null){
                    buffer.append(line);
                }
                return buffer.toString();
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(connection != null){
                    connection.disconnect();
                }
                try {
                    if(reader != null) {
                        reader.close();
                    }
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            return null;
        }
        protected void onPostExecute(String result){
            super.onPostExecute(result);
            Json_String = result;
            Toast.makeText(getContext(),result,Toast.LENGTH_LONG).show();
            mProgressDialog.dismiss();
        }
    }