Well, as far as I can tell, the only error-handling you're doing in
that whole block is to set picvalues to an empty object if you get a
JSONException... If all you're going to do on Exception is log a
message, there's probably no reason to call out each Exception
seperately.  You can Log each step that might throw, and wrap the
whole method in a try/catch.  You'll still be able to pull out
specific types of Exceptions if you need to... Maybe something like
this:


        public void something() {
                try {
                        nameValuePairs.add(new BasicNameValuePair("id", picId));

                        InputStream content = null;
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httpPost = new HttpPost(
                                        
"http://www.pedroteixeira.org/thennnow/alg_showpopup.php";);

                        httpPost.setEntity(new 
UrlEncodedFormEntity(nameValuePairs));
                        response = httpclient.execute(httpPost);
                        content = response.getEntity().getContent();

                        StringBuilder sb = new StringBuilder();
                        String line;
                        BufferedReader reader;

                        reader = new BufferedReader(new 
InputStreamReader(content,
"UTF-8"));

                        while ((line = reader.readLine()) != null) {
                                sb.append(line);
                                String pic_values = sb.toString();
                                try {
                                        picvalues = new JSONArray(pic_values);
                                } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        picvalues = new JSONArray(""); // just 
so it's not null.
                                }
                        }
                } catch (JSONException je) {
                        picvalues = new JSONArray();
                } catch (Exception e) {
                        Log.d("TAG", "Exception:" + e);
                }
        }



On Jul 7, 6:03 am, Pedro Teixeira <pedroteixeir...@gmail.com> wrote:
> I'm having a hard time keeping my code clean... mainlly because all
> the try's and catches...
> I'm simple doing an HTTP request on which the response is then
> converted to JSON Array.. but now it's a mess of {}'s .. is there
> anyway to agregate all this try and catches? The code really looks bad
> and unreadable like this...  and worst, it's executing correctly
> because I'm not finishing in the correct places..
>
> nameValuePairs.add(new BasicNameValuePair("id", picId));
>
>                                         InputStream content = null;
>                                         HttpClient httpclient = new 
> DefaultHttpClient();
>                                         HttpPost httpPost = new 
> HttpPost("http://www.pedroteixeira.org/
> thennnow/alg_showpopup.php");
>
>                                         try {
>                                                                 
> httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
>                                         }
>                                         catch (UnsupportedEncodingException 
> e1) {
>                                                                 // TODO 
> Auto-generated catch block
>                                                                 
> e1.printStackTrace();
>                                         }
>                                         try {
>                                                                 response = 
> httpclient.execute(httpPost);
>                                         } catch (ClientProtocolException e1) {
>                                                                 // TODO 
> Auto-generated catch block
>                                                                 
> e1.printStackTrace();
>                                         } catch (IOException e1) {
>                                                                 // TODO 
> Auto-generated catch block
>                                                                 
> e1.printStackTrace();
>                                         }
>                                         try {
>                                                                 content = 
> response.getEntity().getContent();
>                                         } catch (IllegalStateException e1) {
>                                                                 // TODO 
> Auto-generated catch block
>                                                                 
> e1.printStackTrace();
>                                         } catch (IOException e1) {
>                                                                 // TODO 
> Auto-generated catch block
>                                                                 
> e1.printStackTrace();
>                                         }
>
>                                         StringBuilder sb = new 
> StringBuilder();
>                                         String line;
>                                         BufferedReader reader;
>
>                                         try {
>                                                 reader = new 
> BufferedReader(new InputStreamReader(content,
> "UTF-8"));
>
>                                                 while ((line = 
> reader.readLine()) != null)
>                                                 {
>                                                                 
> sb.append(line);
>                                                                 String 
> pic_values = sb.toString();
>                                                                 try {
>                                                                         
> picvalues = new JSONArray(pic_values);
>                                                                 } catch 
> (JSONException e) {
>                                                                         // 
> TODO Auto-generated catch block
>                                                                         
> picvalues = new JSONArray("");  // just so it's not null.
>                                                                 }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to