如何在安卓工作室的AsyncTask上设置正确的错误消息



我构建了通过API注册新用户的"用户注册表"。我用AsyncTask"POST"方法构建它。但是在我的应用程序中,即使用户没有注册 toast 也会附带"注册成功"消息。我想使用 Toast 显示正确的错误消息,即"注册失败"或其他消息,但我不知道在哪里设置 Toast 消息。这是我的异步任务代码...

public class AsyncCreateacc extends AsyncTask<Void, Void, AsyncTaskResult<Object>>{
    private String user_name = "";
    private String passwordacc= "";
    @Override
    protected void onPreExecute() {
        user_name=username.getText().toString();
        passwordacc=password.getText().toString();
        super.onPreExecute();
    }
    @Override
    protected AsyncTaskResult<Object> doInBackground(Void... params) {
        BufferedReader in = null;
        try {
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
            HttpConnectionParams.setSoTimeout(httpParameters, 20000);
            // 1. create HttpClient
            HttpClient client = new DefaultHttpClient(httpParameters);
            String url = "My_URL";
            // 2. make POST request to the given URL
            HttpPost request = new HttpPost(url);
            // 3. Set some headers to inform server about the type of the content
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");
            request.setHeader("Authorization", "Basic " + "My_key");
            // 4. set httpPost Entity

            JSONObject jsonObject = new JSONObject();
            jsonObject.accumulate("email", user_name);
            jsonObject.accumulate("password",passwordacc);
            jsonObject.accumulate("company_id", "0");
            jsonObject.accumulate("user_type", "C");
            request.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
            //
            // 5. Execute POST request to the given URL
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            String result = sb.toString();
            return new AsyncTaskResult<Object>(result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return new AsyncTaskResult<Object>("");
    }
    @Override
    protected void onPostExecute(AsyncTaskResult<Object> result) {
        super.onPostExecute(result);
        //  findViewById(R.id.rlProgress).setVisibility(View.GONE);
        if (result.getError() != null) {
            Toast.makeText(getApplicationContext(), "Register Unsuccessfull", Toast.LENGTH_LONG).show();
        } else {
            try {
                // hideSoftKeyboard();
                // showSnackBar("Address Updated", findViewById(R.id.activity_edit_address));

                Toast.makeText(getApplicationContext(), "Register Successfully", Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

请做建议。谢谢!

你应该创建新的 AsyncTaskResult 并传递你的异常对象,如下所示:

AsyncTask<String, String, AsyncTaskResult<JSONObject>> jsonLoader = new AsyncTask<String, String, AsyncTaskResult<JSONObject>>() {
        @Override
        protected AsyncTaskResult<JSONObject> doInBackground(
                String... params) {
            try {
                // get your JSONObject from the server
                return new AsyncTaskResult<JSONObject>(JSon Object);
            } catch (Exception E) {
                return new AsyncTaskResult<JSONObject>(E);
            }
        }
        protected void onPostExecute(AsyncTaskResult<JSONObject> result) {
            if ( result.getError() != null ) {
                // error handling here
            } else {
                JSONObject yourResult = result.getResult();
                // result handling here
            }
        };
    }

最新更新