我正在尝试将数据发送到在本地主机上运行的 jsp 文件。当我在手机上单击提交时,应用程序失败,并向我显示不幸的是,应用程序已停止。
登录任务:
public class LoginTask extends AsyncTask<String, Void, Integer> {
private ProgressDialog progressDialog;
private LoginActivity activity;
private int id = -1;
public LoginTask(LoginActivity activity, ProgressDialog progressDialog)
{
this.activity = activity;
this.progressDialog = progressDialog;
}
@Override
protected void onPreExecute()
{
progressDialog.show();
}
@Override
protected Integer doInBackground(String... arg0)
{
String result = "";
int responseCode = 0;
try
{
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.100:8080/test/AndroidCheck.jsp");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", arg0[0]));
nameValuePairs.add(new BasicNameValuePair("password", arg0[1]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
int executeCount = 0;
HttpResponse response;
do
{
progressDialog.setMessage("Logging in.. ("+(executeCount+1)+"/5)");
executeCount++;
response = client.execute(httppost);
responseCode = response.getStatusLine().getStatusCode();
} while (executeCount < 5 && responseCode == 408);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line;
while ((line = rd.readLine()) != null)
{
result = line.trim();
}
id = Integer.parseInt(result);
}
catch (Exception e) {
responseCode = 408;
e.printStackTrace();
}
return responseCode;
}
@Override
protected void onPostExecute(Integer headerCode)
{
progressDialog.dismiss();
if(headerCode == 202)
activity.login(id);
else
activity.showLoginError("");
}
}
登录活动:
public class LoginActivity extends Activity {
protected static final int LOGIN_REQUEST_CODE = 0;
protected static final int RECOVER_REQUEST_CODE = 1;
protected static final int REGISTER_REQUEST_CODE = 2;
public static final int LOGOUT_RESULT_CODE = 2;
SharedPreferences sharedPreferences;
private EditText username;
private EditText password;
private Button btnLogin;;
private final Class<?> LOGIN_DESTINATION = AndroidNavigationTabsActivity.class;
@Override
protected void onCreate(Bundle savedInstanceState)
{
sharedPreferences = getPreferences(MODE_PRIVATE);
super.onCreate(savedInstanceState);
if( sharedPreferences.getBoolean("user_logged_in", false))
{
startActivityForResult(
new Intent(LoginActivity.this, LOGIN_DESTINATION),
LOGIN_REQUEST_CODE);
}
setContentView(R.layout.login_activity);
username=(EditText)this.findViewById(R.id.username);
password=(EditText)this.findViewById(R.id.password);
btnLogin=(Button)this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(loginOnClickListener);
}
protected OnClickListener loginOnClickListener = new OnClickListener()
{
public void onClick(View v)
{
ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Logging in...");
progressDialog.setCancelable(false);
LoginTask loginTask = new LoginTask(LoginActivity.this, progressDialog);
loginTask.execute(username.getText().toString(),password.getText().toString());
}
};
public void showLoginError(String result)
{
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setMessage(R.string.login_invalid_error);
AlertDialog alert = builder.create();
alert.setCancelable(false);
alert.show();
}
public void login(int id)
{
sharedPreferences.edit().putBoolean("user_logged_in", true).commit();
startActivityForResult(
new Intent(LoginActivity.this, LOGIN_DESTINATION),
LOGIN_REQUEST_CODE);
}
}
当我在手机上点击提交时,它显示错误(吐司)。jsp页面(AndroidCheck.jsp)向我展示null
(我试图在网页上显示,我在手机中输入的内容:用户名和登录名)。我不知道,问题出在哪里...