android - GetLogin 类返回值

标签 android

我不知道如何从扩展 AsyncTask 的 GetLogin 类中检索值。这是我的代码:

public class GetLogin extends AsyncTask<String, Void, String> {

private final HttpClient httpclient = new DefaultHttpClient();

final HttpParams params = httpclient.getParams();
HttpResponse response;
private String content =  null;
private Context mContext;

public GetLogin(Context context){

    this.mContext = context;

}

protected String doInBackground(String... urls) {

    String result = null;

    try {

        DefaultHttpClient httpClient=new DefaultHttpClient();

        Log.e("Tag", "url: " + urls[0]);

        //Connect to the server
        HttpGet httpGet = new HttpGet(urls[0].toString());

        //Get the response
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream stream = httpEntity.getContent();

        //Convert the stream to readable format
        result = convertStreamToString(stream);
        Log.e("Tag", "Result: " + result);

        if (result.charAt(1)=='1') {
            return "Login Successful";
        } if (result.charAt(1)=='0') {
            return "Login Failed";
        } else {
            return result.toString();
        }

  //  } catch (ClientProtocolException e) {
  //      Log.w("HTTP2:",e );
 //       content = e.getMessage();
  //      cancel(true);
  //      return "Could not connect to Server";
   //     
   // } catch (IOException e) {
   //     Log.w("HTTP3:",e );
   //     content = e.getMessage();
   //     cancel(true);
   //     return "Could not connect to Server";

    }catch (Exception e) {
        Log.w("HTTP4:",e );
        content = e.getMessage();
        cancel(true);
        return "Could not connect to Server";
    }
}

public static String convertStreamToString(InputStream is) 
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } 
    catch (IOException e) {
        e.printStackTrace();
    } 
    finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

}

这是登录 Activity 的代码

    String url = "http://192.168.1.107:6972/evalservice/checkLogin?name="+tvUsername.getText()+"&pass="+tvPassword.getText();
    new GetLogin(LoginActivity.this).execute(url);

我想做的是添加一个 if 语句来查看登录是否成功,如果成功,则继续下一个 Activity

最佳答案

what i would like to do is put an if statement to see if login was successfull and if so, proceed to the next activity

doInBackground(...) 返回的任何内容都会传递给 AsyncTaskonPostExecute(...) 方法。只需检查该方法中的结果,如果成功,则让它启动下一个 Activity 并finish() 当前的 Activity。

关于android - GetLogin 类返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13596309/

相关文章:

android - 更改 ActionBar 样式 : Bring text underneath icon

android - 在使用 Robolectric 测试 Android 应用程序时,如何创建影子类来解决与自定义属性相关的崩溃问题?

android - 如何在android中进行NFC(近场通信)

java - 如何隐藏主屏幕状态栏?或者一些隐藏而不修改主屏幕的应用程序?

android:layout_marginLeft 指的是父 View 还是周围 View ?

android - 我实际上如何绑定(bind)设备?

android - 在 Jenkins 上构建 Android Studio 项目?缺少 android.compileSdkVersion

java - fragment 替换只有在第二次执行时才起作用

android - 有没有人成功地限制了他们的 WebView 缓存的大小?

android - react native 模态显示超慢 iOS 但不是 Android