我试图在android中创建一个POST请求函数我已经做了一切正确但请求显示错误。这个例子
这是doBackground函数。
//doInBackground
private final String mEmail;
private final String mPassword;
String url = "index.php/services/UserLogin";
LoginTask(String email, String password) {
mEmail = email;
mPassword = password;
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
try {
// Simulate network access.
Thread.sleep(2000);
} catch (InterruptedException e) {
return false;
}
ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", mEmail));
nameValuePairs.add(new BasicNameValuePair("password", mPassword));
//Prepare the Post query
try {
HttpClient clientHttp = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = clientHttp.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
sb.append(line + "\n");
is.close();
String result = sb.toString();
System.out.println(result);
}catch (Exception e){
return false;
}
// TODO: register the new account here.
return true;
}
这是doPost函数:
//onPostExecute
protected void onPostExecute(final Boolean success) {
//LoginTask = null;
//showProgress(false);
if (success) {
finish();
} else {
//password.setError(getString(R.string.error_incorrect_password));
//password.requestFocus();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Erreur !");
builder.setMessage("Les informations entrées sont incorrectes.\nVeuillez réessayer!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
所以你看到我的代码中有任何错误 谢谢你的帮助!