请帮助我纠正我关于如何正确编码将登录到网络的 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();
}
}
}
}