Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:26:34+00:00 2026-05-30T04:26:34+00:00

While debugging a WCF service I came across this issue. I use an HttpURLConnection

  • 0

While debugging a WCF service I came across this issue. I use an HttpURLConnection communicate with the service. While debugging the service, I came across an issue where I had to stop the server while the Android application was waiting for a response, causing the Android application to stop unexpectedly.

What would the best method to deal with a server unexpectedly suddenly stopping and having the android client deal with it gracefully. I have the request/respone working in the background via an AsyncTask:

@Override
    protected JSONObject doInBackground( BLHttpJSONPostModel ...models) {
        JSONObject jsonObject = null;

        BLHttpJSONPostModel postModel = models[0];
        URL url = null;
        byte[] postData = null;
        String protocol = null;
        String response = null;
        InputStream responseStream = null;
        OutputStream outStream = null;
        HttpURLConnection connection = null;
        BufferedReader bufferedReader = null;
        StringBuilder builder = null;

        try {
            url = new URL( postModel.getUrlString() );
            postData = postModel.getPostDataAsJsonObject().toString().getBytes();
            protocol = url.getProtocol();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            return null;
        }

        if ( protocol.equals("https") ) {
            connection = this.setUpHttps( url );    // establishes a secure connection if protocol is HTTPS
        }
        else if ( protocol.equals("http") ) {
            try {
                connection = (HttpURLConnection)url.openConnection();   // establishes un-secured connection
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        else
            return null;    // handles unsupported protocol

        try {
            connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
            connection.setRequestProperty("CONTENT-TYPE", "application/json" );
            connection.setRequestMethod( "POST" );
            connection.setUseCaches( false );
            connection.setDoInput( true );
            connection.setDoOutput( true );
            connection.connect();
            outStream = connection.getOutputStream();
            outStream.write(postData);
            outStream.close();

            String _type = connection.getContentType();         // for debugging only
            String _message = connection.getResponseMessage();  // ""
            int size = connection.getContentLength();           // ""

            bufferedReader = new BufferedReader( new InputStreamReader(connection.getInputStream(), "UTF-8") );
            builder = new StringBuilder();

            while( (response = bufferedReader.readLine()) != null ) {
                builder.append(response);
            }
        } catch (IOException e) {
            e.printStackTrace();   // no exceptio caught here
        } finally {
            connection.disconnect();
        }

        if ( (builder != null) && (! builder.toString().equals("")) ) {
            try {
                jsonObject = new JSONObject( builder.toString() );
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return jsonObject;
    }

Stack trace:

02-20 12:13:33.935: E/AndroidRuntime(1173): FATAL EXCEPTION: AsyncTask #1
02-20 12:13:33.935: E/AndroidRuntime(1173): java.lang.RuntimeException: An error occured while executing doInBackground()
02-20 12:13:33.935: E/AndroidRuntime(1173):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.lang.Thread.run(Thread.java:1096)
02-20 12:13:33.935: E/AndroidRuntime(1173): Caused by: java.lang.NullPointerException
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.io.Reader.<init>(Reader.java:65)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.io.InputStreamReader.<init>(InputStreamReader.java:93)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at com.beslogic.remotpayment.connection.PostJSONTask.doInBackground(PostJSONTask.java:136)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at com.beslogic.remotpayment.connection.PostJSONTask.doInBackground(PostJSONTask.java:1)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-20 12:13:33.935: E/AndroidRuntime(1173):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-20 12:13:33.935: E/AndroidRuntime(1173):     ... 4 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:26:35+00:00Added an answer on May 30, 2026 at 4:26 am

    The answer was a lot simpler (as it usually is).

    Where the BufferedReader is instantiated, since the connection was broken, connection.getInputStream() was returning null.

    It should be:

    responseStream = connection.getInputStream();
    
    if ( responseStream != null ) {
        bufferedReader = new BufferedReader( new InputStreamReader(responseStream, "UTF-8") );
        builder = new StringBuilder();
    
        while( (response = bufferedReader.readLine()) != null ) {
            builder.append(response);
        }
    }
    

    This was throwing a run time exception that was not being caught.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While debugging a crash, I came across this issue in some code: int func()
While debugging some code, I came across an array named default . I thought
I came across the following code while debugging. I was able to get the
While debugging a program using I came across a pecular behavior. I am running
Recently we added the new WCF Routing Service to our project. While debugging a
While debugging crash in a multithreaded application I finally located the problem in this
While debugging an issue with our system, I have discovered a thread contention that
While debugging jQuery apps that use AJAX, I often have the need to see
While debugging slow startup of an Eclipse RCP app on a Citrix server, I
Sometimes while debugging, I need to restart a service on a remote machine. Currently,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.