Java AsyncTask#3 Android在向tomcat服务器发布数据时与doInBackground崩溃

Java AsyncTask#3 Android在向tomcat服务器发布数据时与doInBackground崩溃,java,android,tomcat,android-fragments,android-asynctask,Java,Android,Tomcat,Android Fragments,Android Asynctask,我正在尝试将数据从android发布到应用服务器。当用户注册自己时,他单击注册按钮。然后AsynTask循环从这里开始。之后,该片段应替换为下载片段 下面是Register.java的代码 public class Register extends Fragment{ private Button register; private EditText tcno,username,confirmpassword,password; /** * Define Click lis

我正在尝试将数据从android发布到应用服务器。当用户注册自己时,他单击注册按钮。然后AsynTask循环从这里开始。之后,该片段应替换为下载片段

下面是Register.java的代码

public class Register extends Fragment{

 private Button register;
 private EditText tcno,username,confirmpassword,password;
   /**
     * Define Click listener for the button.
     */
 private static final String SERVICE_URL = "http://10.0.2.2:8084/ClinicWebApp/rest/routine";
 Context thiscontext;

    private OnClickListener registerClickListener = new OnClickListener() {
        public void onClick(View v) {
              SharedPreferences sharedPreferences = PreferenceManager
                        .getDefaultSharedPreferences(v.getContext());
            Editor editor = sharedPreferences.edit();
            editor.putString("verification_code", tcno.toString());
            editor.commit();
            //Dbye kaydet user bilgilerini



            if (tcno.equals("") || username.equals("") ||  
                    password.equals("")||confirmpassword.equals("")) {
              //  Toast.makeText(getActivity().getApplicationContext(), "Please enter in  
                    //all required fields.",
                //        Toast.LENGTH_LONG).show();
                return;
            }
            //post to web

            WebServiceTask wst = new WebServiceTask(WebServiceTask.POST_TASK, 
                getActivity(), "Posting data...");

            wst.addNameValuePair("tcno", tcno.getText().toString());

            wst.execute(new String[] { SERVICE_URL });

            FragmentManager fragmentManager = getFragmentManager();
            DownloadRoutines downloadFragment = new DownloadRoutines();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.frameLayout, downloadFragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.register, null);
        contentView.setDrawingCacheEnabled(false);

        register = (Button)contentView.findViewById(R.id.register);
        tcno=(EditText)contentView.findViewById(R.id.tcnoEditText);
        username=(EditText)contentView.findViewById(R.id.usernameEditText);
        password=(EditText)contentView.findViewById(R.id.passwordEditText);
        confirmpassword=(EditText)contentView.findViewById  
                (R.id.confirmPasswordEditText);
        thiscontext = container.getContext();

        return contentView;
    }

    @Override
    public void onStart() {
        super.onStart();
        register.setOnClickListener(registerClickListener);
    }
    public void handleResponse(String response) {

        System.out.println(response);



    }

    private class WebServiceTask extends AsyncTask<String, Integer, String> {

        public static final int POST_TASK = 1;
        public static final int GET_TASK = 2;

        private static final String TAG = "WebServiceTask";

        // connection timeout, in milliseconds (waiting to connect)
        private static final int CONN_TIMEOUT = 3000;

        // socket timeout, in milliseconds (waiting for data)
        private static final int SOCKET_TIMEOUT = 5000;

        private int taskType = GET_TASK;
        private Context mContext=null;
        private String processMessage = "Processing...";

        private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();

        private ProgressDialog pDlg = null;

        public WebServiceTask(int taskType, Context mcontext, String processMessage) {

            this.taskType = taskType;
            this.mContext = mcontext;
            this.processMessage = processMessage;
        }

        public void addNameValuePair(String name, String value) {

            params.add(new BasicNameValuePair(name, value));
        }

        private void showProgressDialog() {

         //   pDlg = new ProgressDialog(mContext);
          //  pDlg.setMessage(processMessage);
           // pDlg.setProgressDrawable(mContext.getWallpaper());
           // pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
           // pDlg.setCancelable(false);
            //pDlg.show();

        }

        @Override
        protected void onPreExecute() {

          //  hideKeyboard();
           // showProgressDialog();

        }

        protected String doInBackground(String... urls) {

            String url = urls[0];
            String result = "";

            HttpResponse response = doResponse(url);

            if (response == null) {
                return result;
            } else {
                System.out.println(response);
                try {

                    result = inputStreamToString(response.getEntity().getContent());


                } catch (IllegalStateException e) {
                    Log.e(TAG, e.getLocalizedMessage(), e);

                } catch (IOException e) {
                    Log.e(TAG, e.getLocalizedMessage(), e);
                }

            }

            return result;
        }

        @Override
        protected void onPostExecute(String response) {

            handleResponse(response);
            //pDlg.dismiss();

        }

        // Establish connection and socket (data retrieval) timeouts
        private HttpParams getHttpParams() {

            HttpParams htpp = new BasicHttpParams();

            HttpConnectionParams.setConnectionTimeout(htpp, CONN_TIMEOUT);
            HttpConnectionParams.setSoTimeout(htpp, SOCKET_TIMEOUT);

            return htpp;
        }

        private HttpResponse doResponse(String url) {

            // Use our connection and data timeouts as parameters for our
            // DefaultHttpClient
            HttpClient httpclient = new DefaultHttpClient(getHttpParams());

            HttpResponse response = null;

            try {
                switch (taskType) {

                case POST_TASK:
                    HttpPost httppost = new HttpPost(url);
                    // Add parameters
                    httppost.setEntity(new UrlEncodedFormEntity(params));

                    response = httpclient.execute(httppost);
                    break;
                case GET_TASK:
                    HttpGet httpget = new HttpGet(url);
                    response = httpclient.execute(httpget);
                    break;
                }
            } catch (Exception e) {

                Log.e(TAG, e.getLocalizedMessage(), e);

            }

            return response;
        }

        private String inputStreamToString(InputStream is) {

            String line = "";
            StringBuilder total = new StringBuilder();

            // Wrap a BufferedReader around the InputStream
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));

            try {
                // Read response until the end
                while ((line = rd.readLine()) != null) {
                    total.append(line);
                }
            } catch (IOException e) {
                Log.e(TAG, e.getLocalizedMessage(), e);
            }

            // Return full string
            return total.toString();
        }

    }

      }
错误行的原因:

 result = inputStreamToString(response.getEntity().getContent());

原因是什么?为什么应用程序会崩溃?

我怀疑您从服务器收到的响应为空。调查本节:

        HttpResponse response = null;

        try {
            switch (taskType) {

            case POST_TASK:
                HttpPost httppost = new HttpPost(url);
                // Add parameters
                httppost.setEntity(new UrlEncodedFormEntity(params));

                response = httpclient.execute(httppost);
                break;
            case GET_TASK:
                HttpGet httpget = new HttpGet(url);
                response = httpclient.execute(httpget);
                break;
            }
        } catch (Exception e) {

            Log.e(TAG, e.getLocalizedMessage(), e);
        }

您要么从服务器获得空响应,要么在设置响应之前遇到异常。

Register.java的doInBackground(Register.java:176)第176行。。它崩溃为空pointer@MacroAcierno我怎么解决呢?把电话发出去,我们会的see@MarcoAcierno我更新了我的问题。ThanksWell,如果inputStreamToString为null,则表示他没有从http请求中获得任何信息。。或者类似的东西。
        HttpResponse response = null;

        try {
            switch (taskType) {

            case POST_TASK:
                HttpPost httppost = new HttpPost(url);
                // Add parameters
                httppost.setEntity(new UrlEncodedFormEntity(params));

                response = httpclient.execute(httppost);
                break;
            case GET_TASK:
                HttpGet httpget = new HttpGet(url);
                response = httpclient.execute(httpget);
                break;
            }
        } catch (Exception e) {

            Log.e(TAG, e.getLocalizedMessage(), e);
        }