E / AndroidRuntime:FATAL EXCEPTION:AsyncTask#1 java.lang.RuntimeException:执行doInBackground()时发生错误

时间:2015-06-30 21:27:20

标签: android-asynctask

我正在尝试从扩展AsyncTask的类中为post方法发送一些参数,但这不起作用,因为我收到错误。

这是我的logcat

 06-30 20:59:14.159      614-632/com.example.aastudillo.cats
 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
     java.lang.RuntimeException: An error occured while executing doInBackground()
             at android.os.AsyncTask$3.done(AsyncTask.java:278)
             at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
             at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
             at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
             at java.util.concurrent.FutureTask.run(FutureTask.java:137)
             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
             at java.lang.Thread.run(Thread.java:856)
      Caused by: java.lang.NullPointerException
             at com.example.aastudillo.cats.Login$ValidatingLogin.doInBackground(Login.java:110)
             at com.example.aastudillo.cats.Login$ValidatingLogin.doInBackground(Login.java:93)
             at android.os.AsyncTask$2.call(AsyncTask.java:264)
             at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
             at java.util.concurrent.FutureTask.run(FutureTask.java:137)
             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
             at java.lang.Thread.run(Thread.java:856)

login.java

public class Login extends Activity {


    public static final String url = "http://192.168.0.217:80/Services/WebService.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    private ProgressDialog pDialog;
    Button btnLogin;
    Button btnLinkToRegister;
    EditText emailEditText;
    EditText PasswordEditText;
    TextView Vista;

    HttpHandler httpHandler = new HttpHandler();

    @Override
    public void onPause() {
        super.onPause();
        if (pDialog != null) {
            pDialog.dismiss();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        Vista = (TextView) findViewById(R.id.Vista);
        emailEditText = (EditText) findViewById(R.id.email);
        PasswordEditText = (EditText) findViewById(R.id.password);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
        btnLogin.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                btnLogin.setClickable(false);
                new ValidatingLogin().execute();
            }
        });
        btnLinkToRegister.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Login.this, RegisterActivity.class);
                startActivity(i);

            }
        });

    }
        @Override

        public boolean onCreateOptionsMenu (Menu menu){
            getMenuInflater().inflate(R.menu.menu_login, menu);
            return true;

        }


        public class ValidatingLogin extends AsyncTask<String, String, String> {
            int success;

            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(Login.this);
                pDialog.setMessage("Espere...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();

            }


            protected String doInBackground(String... params) {


                String  SendPersonal = emailEditText.getText().toString();
                String SendPassword = PasswordEditText.getText().toString();
                try {
                    List<NameValuePair> Params = new ArrayList <NameValuePair>();
                    Params.add(new BasicNameValuePair("Cod_Personal", SendPersonal));
                    Params.add(new BasicNameValuePair("Password", SendPassword));


                    JSONObject J = httpHandler.makeHttpRequest(url,"POST", Params);

                    success = J.getInt(TAG_SUCCESS);
                    if (success == 1) {

                        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Login.this);
                        SharedPreferences.Editor edit = prefs.edit();
                        edit.putString("Cod_Personal", SendPassword);
                        edit.commit();

                        Intent i = new Intent(Login.this, activity_actividad_principal.class);
                        finish();
                        startActivity(i);
                        return J.getString(TAG_MESSAGE);

                    } else {


                        return J.getString(TAG_MESSAGE);
                    }


                } catch (JSONException e) {
                }
                return null;
            }

            protected void onPostExecute(String file_url) {


                pDialog.dismiss();
                if (file_url != null) {
                    Toast.makeText(getApplicationContext(), file_url, Toast.LENGTH_LONG).show();
                } else {

                }
            }
        }


    }

httphandler.java

public class HttpHandler{

        static InputStream is = null;
        static JSONObject Jobj = null;
        static String json="";

        //Constructor
        public HttpHandler(){


        }


    public JSONObject getJSONFromUrl(final String url){

        try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try{
            String line = null;
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                    while((line = reader.readLine())!=null){

                        sb.append(line+"\n");
                    }
                is.close();
            json= sb.toString();

        }catch (Exception e){
            Log.e("TAG",""+ e.toString());
        }

        try{
            Jobj = new JSONObject(json);

        }catch (JSONException e){

            Log.e("JSON Parser","Error Parsing Data" +e.toString());
        }
        return Jobj;
    }


    public JSONObject makeHttpRequest(String url,String method,List<NameValuePair> Params ){
            try{
                if (method == "POST"){
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(url);
                    httpPost.setEntity(new UrlEncodedFormEntity(Params));

                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();

                }else if (method == "GET"){

                    HttpClient httpClient = new DefaultHttpClient();
                    String ParamString = URLEncodedUtils.format(Params,"utf-8");
                    url += "?" + ParamString;
                    HttpGet httpGet = new HttpGet(url);

                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } 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
        return Jobj;



    }

}

1 个答案:

答案 0 :(得分:0)

你的HttpHandler返回null,导致NPE。它返回null的原因应该是之前的日志,但是你省略了它。

此外,我看到你试图从禁止的后台线程开始或完成一个活动。