0

LoginAuthenticate在执行以下代码时,我在关闭流的同时尝试块中的类中出现 NullPointerException 。

我正在使用 AsyncTask 并从onClick按钮上激活它。

onClick我正在调用登录身份验证并在尝试块中获取异常。

public class DattabActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */    
    private final static String SERVICE_URI = "http://182.50.154.23/Dattab.Device.Rest/ServiceClass/RestDeviceService.svc";

        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("DEBUG", "DattabActivity");        
        setContentView(R.layout.login);
        Button loginbutton=(Button)findViewById(R.id.loginbtn);
        loginbutton.setOnClickListener(this);

        Intent available=new Intent();
        available.setClass(DattabActivity.this, Available_Surveys.class);         

    }

    public void onClick(View v) {
        switch(v.getId()){

        case R.id.loginbtn:
            // TODO Auto-generated method stub
            EditText userid = (EditText)findViewById(R.id.useredt);
            EditText passwd = (EditText)findViewById(R.id.pwdedt);

            Log.i("DEBUG", "DattabActivity Click Button ");

            userid.setText("surveyor1");
            passwd.setText("dev");
            if(userid.getText().toString()=="" ||passwd.getText().toString()==""){

                Toast.makeText(getApplicationContext(), "Please enter your account details", Toast.LENGTH_LONG).show();
            }
            else{
                    String params = SERVICE_URI + "/authenticateuser/"+userid.getText().toString()+"/"+passwd.getText().toString();
                    try {
                            LoginAuthenticate la = new LoginAuthenticate();
                            la.execute(params);
                            la.get();


                        JSONObject json = new JSONObject(new String(la.getStreamBuffer()));
                        Intent available=new Intent();
                        available.setClass(DattabActivity.this, Available_Surveys.class);
                        available.putExtra("userid", json.getString("SurveyorId"));
                        startActivity(available);
                        Toast.makeText(getApplicationContext(), json.getString("SurveyorId"),Toast.LENGTH_LONG).show();
                        Toast.makeText(getApplicationContext(), "Login Clicked", Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }



            }

            break ;
        }

    }
    private class LoginAuthenticate extends AsyncTask<String,Integer,char[]> {
        private char[] streamBuffer ;
        @Override
        protected char[] doInBackground(String... params) {
            android.os.Debug.waitForDebugger();

            String url=params[0];
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);
            try {
                    request.setHeader("Accept", "application/json");
                    request.setHeader("Content-type", "application/json");
                    HttpResponse response = httpClient.execute(request);
                    HttpEntity responseEntity = response.getEntity();

                    // Read response data into buffer
                    char[] buffer = new char[(int)responseEntity.getContentLength()];
                    InputStream stream = responseEntity.getContent();
                    InputStreamReader reader = new InputStreamReader(stream);
                    reader.read(buffer);
                    stream.close();//here it goes in catch NULL Pointer
                    return buffer ;

            }catch(Exception e){
                e.printStackTrace();
                return null;
            }

        }
        protected void onPostExecute(char[] result){
            streamBuffer = result ;
        }

        public char[] getStreamBuffer(){
            return streamBuffer ;
        }
    }
}
4

2 回答 2

0

HTTP 返回空指针异常的原因有很多。您的互联网无法正常工作,您点击的 URL 不存在或不再存在。在您的 try catch 块中,您做错了导致异常的事情,并且您的 catch 块捕获每个异常总是返回空指针。

如果您在模拟器上进行测试,并且您确定 URL 是正确的并且代码中没有任何问题,您可以禁用/启用您的 Internet 连接,清理项目并重建,然后重新启动模拟器。有时它会修复模拟器中的 Internet 问题。

于 2012-08-02T09:17:47.437 回答
0

试试这个来处理空异常错误。

        try {
                request.setHeader("Accept", "application/json");
                request.setHeader("Content-type", "application/json");
                HttpResponse response = httpClient.execute(request);
                HttpEntity responseEntity = response.getEntity();

                // Read response data into buffer
                char[] buffer = new char[(int)responseEntity.getContentLength()];
                try{
                      InputStream stream = responseEntity.getContent();
                       InputStreamReader reader = new InputStreamReader(stream);
                        reader.read(buffer);
                 }
                 finally{
                       stream.close();//here it goes in catch NULL Pointer
                  }
                return buffer ;

        }catch(Exception e){
            e.printStackTrace();
            return null;
        }

链接:https ://stackoverflow.com/a/156889/1503155

于 2012-08-02T07:37:33.840 回答