使用单个按钮从编辑文本发送数据

时间:2014-01-30 13:09:29

标签: android json

![这里有3个编辑文本框。我在哪里使用json检查登录ID和密码详细信息,另一个文本框用于选择服务器地址。唯一的标准是所有这些都应该通过一个按钮即登录按钮来完成。

任何人都可以帮我处理代码] 1   代码如下

package com.example.catxam;


import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;



import com.example.catxam.JSONParser;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.EditText;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

 public class Login extends Activity {

private EditText inputUserid, inputPassword, server;
TextView forgotPassword;
private Button b1;
public String serve;


// Progress Dialog
    private ProgressDialog pDialog;

// JSON parser class
    JSONParser jsonParser = new JSONParser();


    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String Flag = "flag";


protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.login);

    inputUserid = (EditText) findViewById(R.id.Username_edit);
    inputPassword = (EditText) findViewById(R.id.User_password);
    server = (EditText) findViewById(R.id.serverSelection);

    forgotPassword = (TextView) findViewById(R.id.forgotPassword);
    forgotPassword.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent passForget = new Intent(getApplicationContext(),
                    ForgotPassword.class);
            startActivity(passForget);
        }
    });
    b1 = (Button) findViewById(R.id.loginbutton); // login button


    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

                new CreateNewUser().execute();
                new SelectServerAddress().execute();
            }
    });
}

// this class is for selection of the server address
    class SelectServerAddress extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... arg0) {
            return null;


        }

    }

    // this class is for the checking of the user login and password 
    //i.e. of first login and the next consecutive logins
    class CreateNewUser extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Login.this);
        pDialog.setMessage("Checking..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }


    /**
     * Checking creditenials
     * */
    protected String doInBackground(String... args) {

String user = inputUserid.getText().toString();
String pswrd = inputPassword.getText().toString();


//if (serve == "")
    //{

        //serve = "192.168.0.101/gly_prov_V1";
    //}
//else
//{
    //serve = "glydenlewis.esy.es";
//}

// URL to check username & password
final String url_check_user = "http://" + serve +"/gly_prov_V1/android_check.php";  

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uname", user));
params.add(new BasicNameValuePair("psd", pswrd));
params.add(new BasicNameValuePair("server",serve));


// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_check_user,
        "POST", params);

// check log cat from response
Log.d("Create Response", json.toString());

// check for success tag
try {
    int success = json.getInt(TAG_SUCCESS);
    int flag_ck = json.getInt(Flag);
    if (success == 1) {
        if (flag_ck == 0)
        {
            //First Time Login By User
        Intent i = new Intent(getApplicationContext(), UpdateDetails.class);
            startActivity(i);
            finish();   // closing this screen  
        }
        else 
        {
        // successfully login
        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(i);
        finish();   // closing this screen
        }
    } else {
        Toast.makeText(getApplicationContext(), "Wrong Credentials", Toast.LENGTH_SHORT).show();

    }
} catch (JSONException e) {
    e.printStackTrace();
}
return null;

    }

}

   }

1 个答案:

答案 0 :(得分:0)

您可以以串行方式执行任务。将第一个任务的输出作为第二个任务的输入。

但是,如果在您的任务实际运行时销毁活动,则必须实施取消机制。一个简单的方法是将任务引用作为类成员,并在调用activity的onStop()方法时取消它。

    class static SelectServerAddress extends AsyncTask<String, String, String>{

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

         return getAddress(urls[0]);

    }

    @Override
    protected void onPostExecute(String serverAddress) {

         // Call login service
         mLoginTask = new CreateNewUser(serverAddress);
         mLoginTask.execute();

    }


}

编辑:

更新按钮单击此处的侦听器代码:

b1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {

            new SelectServerAdress().execute();

        }
});

然后更新SelectServerAdress类并添加此方法:

@Override
protected void onPostExecute(String serverAddress) {
    serve = serverAddress;
    new SelectServerAddress().execute();
}