我怎么知道服务器的连接是否正确?

时间:2016-01-23 21:14:46

标签: java android android-asynctask androidhttpclient

我登录但我需要知道连接到服务器的连接失败 ,这是我的按钮事件

 login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            usuario=(EditText)findViewById(R.id.etUsuario);
            contrasena=(EditText)findViewById(R.id.etContrasena);
            usuario_ws = usuario.getText().toString();
            pass_ws =  contrasena.getText().toString();

            new HttpAsyncTask().execute(GET());


        }
    });

这是我服务器响应的asynctask

 private class HttpAsyncTask extends AsyncTask<String, Void, String> {

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

        return GET();

    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        //   Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
        //  etResponse.setText(result);
        Integer respuesta_ws = Integer.valueOf(result);

        if (respuesta_ws>=1){
            sesionIn();
        }
        else if (respuesta_ws==0){
            sesionFail();
        }


    }
}
public String GET() {

    String url = "http://"+ippref+":8080/WSAppInventario/webresources/inventario.users/Login/"+usuario_ws+"/"+pass_ws+"";
    String result = "";
    BufferedReader inStream = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpRequest = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpRequest);
        inStream = new BufferedReader(
                new InputStreamReader(
                        response.getEntity().getContent()));

        StringBuffer buffer = new StringBuffer();
        String line = "";
        //String NL = System.getProperty("line.separator");
        while ((line = inStream.readLine()) != null) {
            buffer.append(line);
        }
        inStream.close();
        result = buffer.toString();
        respuesta_ws = Integer.valueOf(result);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

字符串ippref 它是在SharedPreferences

之前保留的对象

1 个答案:

答案 0 :(得分:0)

这应该有效

private class HttpAsyncTask extends AsyncTask<String, Void, String> {

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

    return GET();

}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
    //   Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
    //  etResponse.setText(result);

    if (result=="failed"){
       sesionFail();
    }
    else{
        sesionIn();
    }


}
}
public String GET() {

String url = "http://"+ippref+":8080/WSAppInventario/webresources/inventario.users/Login/"+usuario_ws+"/"+pass_ws+"";
String result = "";
BufferedReader inStream = null;
try {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpRequest = new HttpGet(url);
    HttpResponse response = httpClient.execute(httpRequest);
    inStream = new BufferedReader(
            new InputStreamReader(
                    response.getEntity().getContent()));

    StringBuffer buffer = new StringBuffer();
    String line = "";
    //String NL = System.getProperty("line.separator");
    while ((line = inStream.readLine()) != null) {
        buffer.append(line);
    }
    inStream.close();
    result = buffer.toString();
    respuesta_ws = Integer.valueOf(result);
} catch (Exception e) {

    e.printStackTrace();
    return "failed";
}
return result;
}