[英]Password validation using rest web-services in android
在這里,我遇到密碼驗證問題,即我輸入正確的密碼時卻說密碼不正確。 在這里我正在使用其余的Web服務,下面是我的代碼,請幫助我。
EditText password;
String Passwordstr;
Button btn_go;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
password = (EditText) findViewById(R.id.passET);
btn_go = (Button) findViewById(R.id.btn_go);
btn_go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Passwordstr = password.getText().toString();
if (Passwordstr.isEmpty()) {
Toast.makeText(Main2Activity.this, "Please, Enter Your Password.", Toast.LENGTH_SHORT).show();
} else {
new MyAsyncTask().execute(Passwordstr);
}
}
});
}
private class MyAsyncTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
String res = PostData(params);
return res;
}
public String PostData(String[] args) {
String s = "";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:82/demo/login.php");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
s = readResponse(httpResponse);
} catch (Exception exception) {
}
return s;
}
protected void onPostExecute(String result) {
if (result.equals("true")) {
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
intent.putExtra("Password", Passwordstr);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Password incorrect", Toast.LENGTH_LONG).show();
}
}
public String readResponse(HttpResponse res) {
InputStream is = null;
String return_text = "";
try {
is = res.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
String line = "";
StringBuffer sb = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
return_text = sb.toString();
} catch (Exception e) {
}
return return_text;
}
}
}
我正在開發一個Android應用程序,目的是通過Web服務器連接到網頁,這里我使用的是Rest調用和Java代碼。請幫助我。
在您的帖子執行中,您必須像這樣檢查null值而不是true值
protected void onPostExecute(String result) {
if (result!=null) {
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
intent.putExtra("Password", Passwordstr);
//No Full Name
//MyHomeActivity.putExtra("GetDisplayName",user.getDisplayName());
// MyHomeActivity.putExtra("GetPhotoUrl",user.getPhotoUrl());
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Password incorrect", Toast.LENGTH_LONG).show();
// Hide the progress bar
// progressBar.setVisibility(View.GONE);
}
}
請嘗試執行此操作,因為您沒有使用鍵和值創建發布參數對。
列表名稱ValuePair = new ArrayList(1); nameValuePair.add(new BasicNameValuePair(“ password”,“ password”)));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); HttpResponse響應= httpClient.execute(httpPost);
還可以使用相同的密碼從郵遞員或hurl.it中訪問rest api並檢查響應。
您可以使用android-async-http簡單,快速
並且您可以訪問UI線程和響應方法上的視圖
RestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
// Pull out the first event on the public timeline
JSONObject firstEvent = timeline.get(0);
String Text = firstEvent.getString("text");
textview.setText(Text);
// Do something with the response
System.out.println(tweetText);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.