请帮助我纠正我的错误,即如何正确编码将登录到网络的Android活动,当它成功登录时,它可以开始查看该门户网站的内容?
这是我未完成的代码,请纠正我的错误。提前感谢!
package com.example.weblogin;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
public class LoginActivity extends Activity implements OnClickListener {
private ProgressDialog pDialog; // Progress Dialog
private static final String LOGIN_URL = "http://vo.aimglobalinc.com/control/con_login.asp";
JSONParser jsonParser = new JSONParser(); // JSON parser class
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private EditText user, pass;
private Button mSubmit, mRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
//requestWindowFeature(Window.FEATURE_NO_TITLE);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// get intent data
Intent i1 = getIntent();
// Selected image id
i1.getExtras().getInt("login");
setResult(RESULT_OK, i1);
//setup input fields
user = (EditText)findViewById(R.id.txt_username);
pass = (EditText)findViewById(R.id.txt_password);
//setup buttons
mSubmit = (Button)findViewById(R.id.btn_login);
mRegister = (Button)findViewById(R.id.btn_register);
//register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
new AttemptLogin().execute();
break;
case R.id.btn_register:
//Intent i = new Intent(this, RegisterActivity.class);
//startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
//three methods get called, first preExecture, then do in background, and once do
//in back ground is completed, the onPost execture method will be called.
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uname", username));
params.add(new BasicNameValuePair("pword", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
//Toast.makeText(LoginActivity.this, "Login Successful!", Toast.LENGTH_LONG).show();
//Intent i = new Intent(LoginActivity.this, PortalContents.class);
finish();
//startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
//Toast.makeText(LoginActivity.this, "Login Fail!", Toast.LENGTH_LONG).show();
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(getApplicationContext(), file_url, Toast.LENGTH_LONG).show();
}
}
}
}
您需要配置您的服务器(例如:- 本地 IIS 服务器)以在登录凭据正确时返回响应
我的登录异步任务代码:-
private class GetLogin extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
response = null;
try {
progressDialog = new ProgressDialog(Login.this, android.R.style.Theme_Holo_Light_Dialog);
progressDialog.setMessage("Please wait...");
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if(getLogin !=null && getLogin.getStatus() != AsyncTask.Status.FINISHED){
getLogin.cancel(true);
}
}
});
progressDialog.show();
} catch (Exception e) {
}
}
protected Void doInBackground(Void... params) {
try{
String deviceId = CommonString.deviceId;
String buildVesrsion = CommonString.buildVersion;
HashMap<String, String> map = new HashMap<String, String>();
map.put("email", edtEmail.getText().toString().trim());
map.put("password", edtPassword.getText().toString().trim());
map.put("deviceID", deviceId);
map.put("buildV", buildVesrsion);
map.put("Platform", CommonString.en);
map.put("deviceTokenId", regId);
response = CommonFunction.PostRequest("login", map);
}catch(Exception e){
response = null;
}
return null;
}
@Override
protected void onCancelled() {
}
@Override
protected void onPostExecute(Void result) {
if(response != null && !response.equals("")){
parseResponse(response);
}else{
if(progressDialog != null & progressDialog.isShowing()){
progressDialog.dismiss();
}
Toast.makeText(getApplicationContext(), CommonString.NO_NETWORK, Toast.LENGTH_SHORT).show();
}
}
}
我从服务器得到一个"响应",我使用这个函数解析它:-
private void parseResponse(String responce) {
try {
JSONObject object = new JSONObject(responce);
rCode = Integer.parseInt(object.getString("rcode"));
switch (rCode) {
case 100: // If respose is true do here
isLoginCorrect = true;
JSONObject data = object.getJSONObject("data");
JSONObject userInfo = data.getJSONObject("userInfo");
break;
case 101: // Different errors based on the errors you have defined in your server
isLoginCorrect = false;
message = object.getString("msg");
handler.sendEmptyMessage(0);
break;
case 102:
isLoginCorrect = false;
message = object.getString("msg");
Toast.makeText(Login.this.getApplicationContext(), "There seems to be some trouble with your account, please login again.", Toast.LENGTH_SHORT).show();
handler.sendEmptyMessage(0);
break;
case 103:
isLoginCorrect = false;
message = object.getString("msg");
handler.sendEmptyMessage(0);
break;
case 104:
isLoginCorrect = false;
message = object.getString("msg");
handler.sendEmptyMessage(0);
break;
case 105:
isLoginCorrect = false;
message = object.getString("msg");
handler.sendEmptyMessage(0);
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
isLoginCorrect = false;
}
}
你可以做这样的事情。