1

現在、サーバーに接続できないとアプリがクラッシュします。これを処理するにはどうすればよいですか。代わりに、サーバーがダウンしていることをユーザーに知らせて、もう一度やり直してください。

private void sendPostRequest(String givenEmail, String givenPassword) {

    class SendPostRequestTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            String emailInput = params[0];
            String passwordInput = params[1];

            String jsonUserInput = "{email: " + emailInput + ", password: "
                    + passwordInput + "}";

            try {

                HttpClient httpClient = new DefaultHttpClient();

                // Use only the web page URL as the parameter of the
                // HttpPost argument, since it's a post method.
                HttpPost httpPost = new HttpPost(SERVER_URL);

                // We add the content that we want to pass with the POST
                // request to as name-value pairs

                json = new JSONObject(jsonUserInput);

                jsonString = new StringEntity(json.toString());

                httpPost.setEntity(jsonString);

                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");

                HttpParams httpParameters = httpPost.getParams();
                // Set the timeout in milliseconds until a connection is established.
                int timeoutConnection = 1000;
                HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                // Set the default socket timeout (SO_TIMEOUT) 
                // in milliseconds which is the timeout for waiting for data.
                int timeoutSocket = 1000;
                HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

                // HttpResponse is an interface just like HttpPost.
                // Therefore we can't initialize them
                HttpResponse httpResponse = httpClient.execute(httpPost);

                // According to the JAVA API, InputStream constructor does
                // nothing.
                // So we can't initialize InputStream although it is not an
                // interface
                InputStream inputStream = httpResponse.getEntity()
                        .getContent();

                InputStreamReader inputStreamReader = new InputStreamReader(
                        inputStream);

                BufferedReader bufferedReader = new BufferedReader(
                        inputStreamReader);

                StringBuilder stringBuilder = new StringBuilder();

                String bufferedStrChunk = null;

                while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
                    stringBuilder.append(bufferedStrChunk);
                }

                return stringBuilder.toString();

            } catch (ClientProtocolException cpe) {
                Log.i(LOGIN, "ClientProtocolException");
                cpe.printStackTrace();
            } catch (ConnectTimeoutException e) {
                e.printStackTrace();
            } catch (IOException ioe) {
                Log.i(LOGIN, "IOException");
                ioe.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            Log.i(LOGIN, result);

            try {
                serverResponse = new JSONObject(result);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                if ((serverResponse.has("status"))
                        && (serverResponse.get("status").toString()
                                .equals("200"))) {
                    Toast.makeText(getApplicationContext(), "SUCCESS!",
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Incorrect Email/Password!!!",
                            Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    SendPostRequestTask sendPostRequestTask = new SendPostRequestTask();
    sendPostRequestTask.execute(givenEmail, givenPassword);
}

LogCat エラー ログ

11-11 16:26:14.970: I/R.id.login_button(17379): IOException 11-11 16:26:14.970: W/System.err(17379): org.apache.http.conn.HttpHostConnectException: 接続http:// へ*拒否された 11-11 16:26:14.980: W/System.err(17379): org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183) で

4

3 に答える 3

0

droidQueryを使用してすべてを簡素化し、HTTP エラー処理を含めることができます。

$.ajax(new AjaxOptions().url("http://www.example.com")
                        .type("POST")
                        .dataType("json")
                        .header("Accept", "application/json")
                        .header("Content-type", "application/json")
                        .timeout(1000)
                        .success(new Function() {
                            @Override
                            public void invoke($ d, Object... args) {
                                Toast.makeText(this, "SUCCESS!", Toast.LENGTH_SHORT).show();
                                JSONObject serverResponse = (JSONObject) args[0];
                                //handle response
                            }
                        })
                        .error(new Function() {
                            @Override
                            public void invoke($ d, Object... args) {
                                AjaxError error = (AjaxError) args[0];
                                //toast shows the error code and reason, such as "Error 404: Page not found"
                                Toast.makeText(this, "Error " + error.status + ": " + error.reason, Toast.LENGTH_SHORT).show();
                            }
                        }));
于 2013-11-12T14:11:07.503 に答える