Android - 如何在异常到来时执行剩余代码

时间:2014-07-09 13:00:46

标签: android exception facebook-sdk-3.0

当我尝试从facebook登录时收到电子邮件。我有例外:没有电子邮件的价值。然后剩下的代码没有执行。我的意思是HTTPParams代码没有执行。我给了电子邮件权限以获取电子邮件但有些用户的facebook电子邮件没有收到。这是我的代码

                      try {

                        JSONObject jobj = new JSONObject(responseData);
                        Dataengine.firstname = jobj.getString("first_name");
                        Dataengine.last_name = jobj.getString("last_name");
                        Dataengine.username = jobj.getString("username");
                        Dataengine.gender = jobj.getString("gender");
                        Dataengine.email = jobj.getString("email");
                        Dataengine.id = jobj.getString("id");
                        try{
                            JSONObject jsonob=jobj.getJSONObject("location");
                            String addres=jsonob.getString("name");
                            MyLog.i("Login","==============address-----------"+addres);
                            String name[]= new String[2];
                            name=addres.split(",");
                            MyLog.i("Login","==============address-----------"+name.length);
                            if(name.length>=2){
                                Dataengine.city=name[0];
                                Dataengine.state=name[1];
                            }
                        }catch(Exception e){
                            Dataengine.email=Dataengine.id+"@facebook.com";

                        }


                       HttpParams signparams = new BasicHttpParams();
                        signparams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
                                HttpVersion.HTTP_1_1);
                        mHttpClient11 = new DefaultHttpClient(signparams);

                        try {

                            HttpPost httppost = new HttpPost(Dataengine.reg);

                            // File ff=new File(Dataengine.profile_image);

                            MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                            multipartEntity.addPart("fname", new StringBody(
                                    Dataengine.firstname));
                            multipartEntity.addPart("lname", new StringBody(
                                    Dataengine.last_name));
                            multipartEntity.addPart("uname", new StringBody(
                                    Dataengine.username));
                            multipartEntity.addPart("pwd", new StringBody(
                                    "password"));
                            multipartEntity.addPart("email", new StringBody(
                                    Dataengine.email));
                            multipartEntity.addPart("gender", new StringBody(
                                    Dataengine.gender));
                            if(null!=Dataengine.state)
                              multipartEntity.addPart("state", new StringBody(Dataengine.state));

                            if(null!=Dataengine.city)
                                multipartEntity.addPart("city", new StringBody(Dataengine.city));

                            multipartEntity.addPart("facebook_id", new StringBody(
                                    Dataengine.id));
                            multipartEntity.addPart("devicetoken", new StringBody(Dataengine.registrationId));
                            multipartEntity.addPart("image", new FileBody(file));
                            httppost.setEntity(multipartEntity);
                            response = mHttpClient11.execute(httppost);
                            entity = response.getEntity().getContent();
                            Log.i("info", "input stream respo entity ...:" + entity);
                            /* String responseString = EntityUtils.toString(response.getEntity());
                            Log.v("", "facebook reg response..:"+responseString);*/


                            //Log.i("info", "ajhdfjaslk88888888888============88888888dfa"+responseString);
                        } catch(ConnectTimeoutException es){
                            es.printStackTrace();
                        }catch (ClientProtocolException e1) {

                            e1.printStackTrace();
                        } catch (IOException e1) {

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

                        }

1 个答案:

答案 0 :(得分:0)

您可以将try catch块与finally语句一起使用。执行try-catch块直到捕获到异常,然后执行finally块。

try {// executes until exception

    } catch ( ){ // executes only if exception is thrown

    } finally { // always gets called

    }