使用Android应用

时间:2015-12-15 18:19:24

标签: android json api

我正在尝试使用andorid中的此代码登录到web服务

private void normalLogin() {

    if (mAsyncHttpClient == null) {
        mAsyncHttpClient = new AsyncHttpClient();
    }

    // Cria um loading
    final ProgressDialog mProgress = new ProgressDialog(LoginActivity.this);
    mProgress.setMessage("Carregando");
    mProgress.setCancelable(false);
    mProgress.show();

    // Parametros
    RequestParams mParams = new RequestParams();
    mParams.add("username", "" + txtEmail.getText().toString());
    mParams.add("password", "" + txtSenha.getText().toString());

    mAsyncHttpClient.post(ServerURLS.SERVER_DOMAIN + ServerURLS.LOGIN,
            mParams, new JsonHttpResponseHandler() {

                @Override
                public void onFailure(int statusCode, Header[] headers,
                                      String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString,
                            throwable);
                    // Falha HTML
                    mProgress.dismiss();
                    Toast.makeText(
                            LoginActivity.this,
                            "Alguma falha aconteceu, tente novamente ou mais tarde.",
                            Toast.LENGTH_LONG).show();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers,
                                      Throwable throwable, JSONObject errorResponse) {
                    super.onFailure(statusCode, headers, throwable,
                            errorResponse);
                    mProgress.dismiss();
                    Toast.makeText(
                            LoginActivity.this,
                            "Alguma falha aconteceu, tente novamente ou mais tarde.",
                            Toast.LENGTH_LONG).show();
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers,
                                      JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    mProgress.dismiss();

                    Log.v("Response", "" + response);

                    try {
                        // Usuario logado corretamente
                        if (!response.getBoolean("ERROR")) {

                            String mJson = response.toString();

                            // Salva os dados do JSON
                            mPrefEditor.putString("json_login", mJson);
                            mPrefEditor.putString("login_password",
                                    txtSenha.getText().toString());
                            mPrefEditor.putBoolean("logado", true);
                            mPrefEditor.commit();

                            // Troca a Activity
                            Intent intent = new Intent(LoginActivity.this,
                                    AfterLoginActivity.class);
                            startActivity(intent);

                            LoginActivity.this.finish();

                        } else {

                            AlertDialog.Builder mAlert = new AlertDialog.Builder(
                                    LoginActivity.this);

                            mAlert.setCancelable(false);
                            mAlert.setTitle("Erro ao tentar efetuar o Login");
                            mAlert.setMessage(""
                                    + response.getString("MESSAGE"));

                            mAlert.setNeutralButton("OK",
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            dialog.dismiss();
                                        }
                                    });
                            mAlert.show();
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            });
}

按钮代码只是一个简单的函数调用,如normallogin();

我不知道错误在哪里,有人可以帮助我吗?

抱歉我的英语不好,我希望你能理解我。

Log Cat

    12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime: FATAL EXCEPTION: main
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime: java.lang.NullPointerException
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes.LoginActivity.normalLogin(LoginActivity.java:81)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes.LoginActivity$2.onClick(LoginActivity.java:62)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at android.view.View.performClick(View.java:4204)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:17355)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:725)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-15 18:23:13.744 12390-12390/prosoft.wolterskluer.com.wolterskluweragendadeobrigacoes E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案