当前位置: 首页 > 知识库问答 >
问题:

执行doinbackground时出错

郏博瀚
2023-03-14

执行 doinBackground() 时出错。

class PostComment extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AddComment.this);
        pDialog.setMessage("Attempting login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        //postData("deepika");
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;
        String post_title = intime.getText().toString();
        String post_message = outtime.getText().toString();
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(AddComment.this);
        String post_username = sp.getString("username", "anon");
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", post_username));
            params.add(new BasicNameValuePair("intime", post_title));
            params.add(new BasicNameValuePair("outtime", post_message));

            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(POST_COMMENT_URL, "POST",
                    params);
            //JSONObject json1 = jsonParser.makeHttpRequest("http://192.168.10.30/webservice/comments.php", "POST",
                //  params);
            // check your log for json response
            Log.d("Login attempt", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
        if (success == 1) {     

                Log.d("Attendence Marked!", json.toString());    
                //finish();
                return json.getString(TAG_MESSAGE);
            }else{
                Log.d("Attendence Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);

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

        return null;

    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        finish();
        if (file_url != null) {
            Toast.makeText(AddComment.this, file_url, Toast.LENGTH_LONG).show();
        }
    }
} 

这是错误日志:

11-05 05:03:20.171:E/Android runtime(3171):致命异常:async task # 1 < br > 11-05 05:03:20.171:E/Android runtime(3171):Process:com . example . MySQL test,PID:3171 < br > 11-05 05:03:20.171:E/Android runtime(3171):Java . lang . runtime EXCEPTION:An

共有1个答案

雍骏俊
2023-03-14

在点击监听器中执行此操作

 mSubmit.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View vw) {
                    String post_title = intime.getText().toString();
                    String post_message = outtime.getText().toString();

                    new PostComment(post_title,post_message).execute();
                }
            }); 



and in the asynch task change this
class PostComment extends AsyncTask<String, String, String> {

            String response = "";
            String post_title ="";
            String post_message="";
            // Check for success tag
            int success;


            public PostComment(String title,String msg) {
                post_title = title;
                post_message = msg;
            }

      @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AddComment.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            //postData("deepika");
        }



     @Override
        protected String doInBackground(String... args) {


            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(AddComment.this);
            String post_username = sp.getString("username", "anon");
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", post_username));
                params.add(new BasicNameValuePair("intime", post_title));
                params.add(new BasicNameValuePair("outtime", post_message));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(POST_COMMENT_URL, "POST",
                        params);
                //JSONObject json1 = jsonParser.makeHttpRequest("http://192.168.10.30/webservice/comments.php", "POST",
                    //  params);
                // check your log for json response
                Log.d("Login attempt", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);

    if (success == 1) {

                    Log.d("Attendence Marked!", json.toString());    

                    response = json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Attendence Failure!", json.getString(TAG_MESSAGE));
                    response = json.getString(TAG_MESSAGE);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }

            return response;

        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();

            if (file_url != null) {
                Toast.makeText(AddComment.this, file_url, Toast.LENGTH_LONG).show();
            }


        }

}
 类似资料: