java - 从未使用过 Onpostexecute 方法

标签 java android json

我收到“从未使用过 OnPostExecute 方法”错误,因此我的对话框永远不会关闭,不确定我的错误在哪里。我确定某处的括号是错误的,但我已经尝试了一段时间但我不能,也许在这种情况下一双新的眼睛会有所帮助。

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_display_all_bets);


            name = (getIntent().getExtras().getString("user")).toLowerCase();
            Log.d("name", name);
            // Hashmap for ListView
            bet = new ArrayList<HashMap<String, String>>();

            // Loading products in Background Thread
            new LoadAllGames().execute();

        }
            /**
             * Background Async Task to Load all product by making HTTP Request
             */
            class LoadAllGames extends AsyncTask<String, String, String> {
                private String id;
                private String stake;
                private String user;
                private String returns;
                private String teams;
                private String status;


                //  *//**
                //   * Before starting background thread Show Progress Dialog
                //    *//*
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(DisplayAllBets.this);
                    pDialog.setMessage("Loading Games. Please wait...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(false);
                    pDialog.show();
                }

                //  *//**
                //    * getting All products from url
                //    *//*
                protected String doInBackground(String... args) {
                    // Building Parameters
                    HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost(url_all_games);
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("email", name));
                    try {
                        post.setEntity(new UrlEncodedFormEntity(params));
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                    try {
                        HttpResponse response = client.execute(post);
                        Log.d("Http Post Response:", response.toString());
                        HttpEntity httpEntity = response.getEntity();
                        InputStream is = httpEntity.getContent();
                        JSONObject jObj = null;
                        String json = "";
                        try {
                            BufferedReader reader = new BufferedReader(new InputStreamReader(
                                    is, "iso-8859-1"), 8);
                            StringBuilder sb = new StringBuilder();
                            String line = null;
                            while ((line = reader.readLine()) != null) {

                                if (!line.startsWith("<", 0)) {
                                    if (!line.startsWith("(", 0)) {
                                        sb.append(line + "\n");
                                    }
                                }
                            }

                            is.close();
                            json = sb.toString();
                            json = json.substring(json.indexOf('{'));
                            Log.d("sb", json);

                        } catch (Exception e) {
                            Log.e("Buffer Error", "Error converting result " + e.toString());
                        }

                        // try parse the string to a JSON object
                        try {
                            jObj = new JSONObject(json);
                        } catch (JSONException e) {
                            Log.e("JSON Parser", "Error parsing data " + e.toString());
                        }

                        // return JSON String
                        Log.d("json", jObj.toString());
                        try {
                            allgames = jObj.getJSONArray(TAG_BET);
                            Log.d("allgames", allgames.toString());
                            // looping through All Products
                            for (int i = 0; i < allgames.length(); i++) {
                                JSONObject c = allgames.getJSONObject(i);

                                // Storing each json item in variable
                                String id = c.getString(TAG_ID);
                                String user = c.getString(TAG_USER);
                                String returns = c.getString(TAG_RETURNS);
                                String stake = c.getString(TAG_STAKE);
                                String status = c.getString(TAG_STATUS);
                                String Teams = c.getString(TAG_TEAMS);
                                Log.d("id", id);
                                Log.d("user", user);
                                Log.d("returns", returns);
                                Log.d("stake", stake);
                                Log.d("status", status);
                                Log.d("teams", Teams);


                                // creating new HashMap
                                HashMap<String, String> map = new HashMap<String, String>();

                                // adding each child node to HashMap key => value
                                map.put(TAG_ID, id);
                                map.put(TAG_TEAMS, Teams);
                                map.put(TAG_USER, user);
                                map.put(TAG_RETURNS, returns);
                                map.put(TAG_STAKE, stake);
                                map.put(TAG_STATUS, status);


                                // adding HashList to ArrayList
                                bet.add(map);
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }


                    return "";
                }


                @Override
                protected void onPostExecute() {
                    // dismiss the dialog after getting all products
                    // updating UI from Background Thread
                    pDialog.dismiss();


                }

            }


            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu_display_all_bets, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();

                //noinspection SimplifiableIfStatement
                if (id == R.id.action_settings) {
                    return true;
                }

                return super.onOptionsItemSelected(item);
            }
        }

最佳答案

您将 AsyncTask 定义为,

class LoadAllGames extends AsyncTask<String, String, String>

为此,你应该有一个 onPostExecute(String) 类型

@Override
    protected void onPostExecute(String param) {
    // dismiss the dialog after getting all products
    // updating UI from Background Thread
    pDialog.dismiss();
}               

应该可以。

关于java - 从未使用过 Onpostexecute 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30561536/

相关文章:

java - 使用 Jersey Apache Client 4 的多线程连接管理器

java - 如何捕获 Room 持久性库中未处理的异常

javascript - 如何将外部 php 文件字符串分配给 javascript 变量?

c# - 如何在 Web API Controller 中接收字节数组和 json

java - Java中同步块(synchronized block)的简化

java - mvn 集成测试与 mvn 故障安全 :integration-test

java - 如何使用 Spinner 下拉菜单更改 TextView 小部件上的文本

android - 我的 ViewPagerIndicator 在哪里?

android - 我应该在什么上下文中使用 AlertDialog.Builder?

javascript - 遍历json对象并根据键名将子obj属性移动到父对象