0

我正在实现一个应用程序,它访问一个 Web 服务来验证用户,但是我遇到了问题,这个 Web 服务工作,经过测试,但是每次我输入电子邮件和密码登录时,应用程序都会中断。

活动

package com.inform.edufindme.activity;
import com.inform.edufindme.R;
import com.inform.edufindme.task.ValidarUsuarioTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;


public class LoginActivity extends Activity {


Button button_login;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    button_login = (Button)findViewById(R.id.button_login);
    button_login.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            EditText editTextLogin = (EditText)findViewById(R.id.edit_text_email);
            EditText editTextSenha = (EditText)findViewById(R.id.edit_text_senha);

            //new ValidarUsuarioTask(LoginActivity.this).execute("vilker","12345");
            new ValidarUsuarioTask(LoginActivity.this).execute(editTextLogin.getText().toString(),editTextSenha.getText().toString());
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

}

任务

package com.inform.edufindme.task;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.inform.edufindme.activity.EduMailActivity;
import com.inform.edufindme.activity.LoginActivity;
import com.inform.edufindme.utils.ConfiguracaoServidor;
import com.inform.edufindme.utils.ConverterStream;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;

public class ValidarUsuarioTask extends AsyncTask<String, Void, HttpResponse>{

private String recurso = "Validar_Usuario/Usuario/";
private Context contexto;


public ValidarUsuarioTask(Context contexto) {
    this.contexto = contexto;
}

@Override
protected HttpResponse doInBackground(String... params) {
    String email = params[0];
    String senha = params[1];
    return autenticarUsuario(email, senha);
}

@Override
protected void onPostExecute(HttpResponse response) {
    HttpEntity entity = response.getEntity();
    Integer statusAutenticacao;
    String conteudoRequisicao = "";

    try {
        conteudoRequisicao = ConverterStream.convertInputStreamToString(entity.getContent());
    }catch (Exception e) {
        e.printStackTrace();
    }
    statusAutenticacao = getStatusValidacao(conteudoRequisicao);

    if(statusAutenticacao.equals(0)){
        Intent intent = new Intent(contexto, EduMailActivity.class);
        contexto.startActivity(intent);
    }else{
        Intent intent = new Intent(contexto, EduMailActivity.class);
        contexto.startActivity(intent);
    }
}

private HttpResponse autenticarUsuario(String email, String senha) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(ConfiguracaoServidor.getInstancia().getURL()+recurso+email+"/"+senha);

    HttpResponse response = null;
    try {
        response = httpclient.execute(httpget);
    } catch (IOException e) {
        System.out.println(e);
    }

    return response;

}

private Integer getStatusValidacao(String conteudo){
    JSONArray result;
    JSONObject validacao;
    Integer statusValidacao = 0;

    try {
        result = new JSONObject(conteudo).getJSONArray("result");
        validacao = (JSONObject)result.get(0);
        statusValidacao = validacao.getInt("VALIDACAO");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return statusValidacao;

}

}

服务器配置

package com.inform.edufindme.utils;
public class ConfiguracaoServidor {

    private  String server = "172.20.10.9";
    private  String port = "2000";

    private static ConfiguracaoServidor instancia = new ConfiguracaoServidor();

    private ConfiguracaoServidor() {
        super();
    }

    public static ConfiguracaoServidor getInstancia() {
        return instancia;
    }

    public String getURL(){

        return new StringBuilder().append("http://").append(server).append(":").append(port).append("/").toString();
    }

}

日志猫

10-21 23:42:43.121: W/dalvikvm(1459): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-21 23:42:43.141: E/AndroidRuntime(1459): FATAL EXCEPTION: main
10-21 23:42:43.141: E/AndroidRuntime(1459): java.lang.NullPointerException
10-21 23:42:43.141: E/AndroidRuntime(1459):     at com.inform.edufindme.task.ValidarUsuarioTask.onPostExecute(ValidarUsuarioTask.java:42)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at com.inform.edufindme.task.ValidarUsuarioTask.onPostExecute(ValidarUsuarioTask.java:1)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at android.os.Looper.loop(Looper.java:137)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at java.lang.reflect.Method.invokeNative(Native Method)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at java.lang.reflect.Method.invoke(Method.java:525)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-21 23:42:43.141: E/AndroidRuntime(1459):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

您的代码失败的原因是 response.getEntity() 返回 null。您应该在访问实体之前检查 null。至于为什么返回null,请尝试检查状态码。

于 2013-10-22T05:10:46.313 回答