开发者

Android display my own error message ifrom an exception

i have implemented a try block that does some network requests but whenever it throws an exception such has a Timeout one, the android application displays a message Saying

Sorry: The application has stopped unexpectedy, please try again.

the only option presented to the user is a force close button. i want to catch this exception and simple display my own error message and let the user try the connection again instead of the user having to close the app.

Here is the code below:

all i want to do is return null after a exception is caught and what ever class that calls this will do it its own error handling like this:

if(postRequest == null){ display error message}

private HttpResponseObject PostRequest() {

        //int response = 0;

        String responseBody = "";
        HttpResponseObject response = new HttpResponseObject();

        try {

            Log.d(TAG, "IN POST_REQUEST_METHOD");
            Log.d(TAG, "URL : " + url);

            httpclient = new DefaultHttpClient();

            httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1);
            //httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
            httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5);
            Log.d(TAG, "httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);");
            post = new HttpPost(url);
//          ClientConnectionRequest connRequest = new ManagedClientConnection();
//          post.setConnectionRequest((ClientConnectionRequest) connRequest.getConnection(3000, TimeUnit.MILLISECONDS));

            ByteArrayEntity entity = new ByteArrayEntity(data.getBytes());
            entity.setChunked(true);

            post.setEntity(new StringEntity(data));

            HttpResponse httpResponse = httpclient.execute(post);

            HttpEntity resEntity = httpResponse.getEntity();


            if (resEntity != null) {
                // indicate that the content of this entity is no longer
                // required
                response.setResponseBody(EntityUtils.toString(resEntity));

                Header[] header = httpResponse.getAllHeaders();

                Log.d(TAG, "httpResponse.getAllHeaders() = " + header[0].getName());
                response.setResponseHeader(httpResponse.getAllHeaders().toString());
                resEntity.consumeContent();
            }

            // release all recources from the httpClient object
            httpclient.getConnectionManager().shutdown();

            response.setResponseCode(httpResponse.getStatusLine().getStatusCode() ) ;

            Log.d(TAG, "responseBody = " + response.getResponseBody());
            Log.d(TAG, "response code = " + response.getResponseCode());
            Log.d(TAG, "response header = " + response.getResponseHeader());

            return response;

        } catch (SocketTimeoutException e) {
            e.printStackTrace();
            Log.d(TAG, "SocketTimeoutException e  = " + e.toString());
            return开发者_如何学Python null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d(TAG, "IOException e  = " + e.toString());
            return null;
        }

    }


The Force Close dialog appears due to an uncaught exception. Since you are not throwing a checked exception yourself, this is due to a runtime exception, most probably a Null Pointer one. This means that there is bug in your code that causes this and not a network error.

In order to find the problem you need to know the type of the Exception being thrown and its message. You can see these at the LogCat - if you are using Eclipse it is available in the debug perspective.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜