我有一个安卓应用程序。我必须通过带有两个参数的 httppost 从服务器获取数据。但我每次都得到响应代码 401。应该是200。
这是我的日志文件:
这是我的活动:
public class LoginActivity extends Activity implements OnClickListener {
Button login;
EditText user, pass;
CheckBox ck;
String FILENAME = "check";
String checkenords;
String FILETOKEN = "token";
String tokenStr;
String FILEEmail = "email";
String emailStr;
String responseStr;
String usernamefromuser;
int responsecode;
String passfromuser;
ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sign_in);
user = (EditText) findViewById(R.id.editText1);
pass = (EditText) findViewById(R.id.editText2);
ck = (CheckBox) findViewById(R.id.checkBox1);
login = (Button) findViewById(R.id.btnlogin);
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
usernamefromuser = user.getText().toString();
passfromuser = pass.getText().toString();
Log.e("successss", "888888888888");
Log.e("Username", "User:" + usernamefromuser);
Log.e("Password", "pass:" + passfromuser);
HttpClient client = new DefaultHttpClient();
String url = "http://54.228.199.162/api/auth";
HttpPost httppost = new HttpPost(url);
// httppost.setHeader("Content-type", "application/json");
// httppost.setHeader("Accept", "application/json");
try {
List<NameValuePair> namevalpair = new ArrayList<NameValuePair>();
namevalpair.add(new BasicNameValuePair("pass", passfromuser));
namevalpair.add(new BasicNameValuePair("email",
usernamefromuser));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
namevalpair, HTTP.UTF_8);
httppost.setEntity(entity);
HttpResponse httpresponse = client.execute(httppost);
responsecode = httpresponse.getStatusLine().getStatusCode();
responseStr = EntityUtils.toString(httpresponse.getEntity());
Log.d("Authentication", "" + responsecode);
// Log.d("httpresponseeeee", httpresponse.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
;
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (responsecode == 200) {
tokenStr = responseStr;
emailStr = usernamefromuser;
try {
FileOutputStream fos = openFileOutput(FILETOKEN,
Context.MODE_PRIVATE);
fos.write(tokenStr.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileOutputStream fos = openFileOutput(FILEEmail,
Context.MODE_PRIVATE);
fos.write(emailStr.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ck.isChecked()) {
checkenords = "enable";
try {
FileOutputStream fos = openFileOutput(FILENAME,
Context.MODE_PRIVATE);
fos.write(checkenords.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent inteGps = new Intent(LoginActivity.this, Gps.class);
startActivity(inteGps);
finish();
} else {
Intent inteGps = new Intent(LoginActivity.this, Gps.class);
startActivity(inteGps);
finish();
}
} else if (responsecode == 401) {
Toast.makeText(getApplicationContext(), getApplicationContext().getResources().getString(R.string.userwrong),
Toast.LENGTH_LONG).show();
} else{
Toast.makeText(getApplicationContext(), getApplicationContext().getResources().getString(R.string.tryagain), Toast.LENGTH_LONG).show();
}
pDialog.dismiss();
}
}
}
用邮递员编辑