나는 안드로이드 응용 프로그램에 초보자입니다. 이미 웹 사이트에서 등록 된 사용자 만 로그인 할 수있는 로그인 페이지를 만들었습니다. 사용자가 자신의 사용자 이름과 암호를 입력하면 등록 된 사용자를 인증하려고합니다. 사용자 이름과 암호를 인증하려면 어떻게해야합니까? 나는 다음 코드를 사용하여 이것을보고 도와 줘. 로그인 버튼을 클릭하면 오류가 표시됩니다.로그인 웹 사이트에서 등록 된 사용자에 대해서만 인증
Main.java
public class Main extends Activity implements OnClickListener {
Button ok,back,exit;
TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Login button clicked
ok = (Button)findViewById(R.id.btn_login);
ok.setOnClickListener(this);
result = (TextView)findViewById(R.id.lbl_result);
}
public void postLoginData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
/* login.php returns true if username and password is equal to saranga */
HttpPost httppost = new HttpPost("http://yoursite.com/wp-login.php");
try {
// Add user name and password
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("", str);
if(str.toString().equalsIgnoreCase("true"))
{
Log.w("", "TRUE");
result.setText("Login successful");
}else
{
Log.w("", "FALSE");
result.setText(str);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
public void onClick(View view) {
if(view == ok){
postLoginData();
}
}
}
오류
Execute HTTP Post Request
Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
<html><head> <title>yoursite.com: The Leading Website Site on the Net</title> <script type="text/javascript">; if(self != top) top.location.href = 'http://'+location.hostname+'/?redir=frame& uid=yoursite50a47fe3380989.09080642'; </script> <script type="text/javascript" src="http://return.bs.domainnamesales.com /return_js.php?d=yoursite.com&s=1352957923"></script> </head> <frameset cols="1,*,1" border=0> <frame name="top" src="tg.php?uid=yoursite50a47fe3380989.09080642" scrolling=no frameborder=0 noresize framespacing=0 marginwidth=0 marginheight=0> <frame src="search.php?uid=yoursite50a47fe3380989.09080642" scrolling="auto" framespacing=0 marginwidth=0 marginheight=0 noresize> <frame src="page.php?yoursite50a47fe3380989.09080642"></frame> </frameset> <noframes> yoursite.com has been connecting our visitors with providers of Computer Networking, Dedicated Web Servers, Email Domain Hosting and many other related services for nearly 10 years. Join thousands of satisfied visitors who found Ftp Hosts, Ftp Servers, Host, Internet Servers, and Linux Servers.<br/> </noframes></html>
FALSE
JSONObject json_data = new JSONObject (result); // 출력을 포함하는 문자열 var. my_output_one = json_data.getString ("var_1"); // 귀하의 응답은 var form web입니다. my_output_two = json_data.getString ("var_2"); – SRY
위의 예는 앱 – SRY
앱을 닫고 데이터가 웹으로 전송되는지 확인합니다. 그렇지 않으면 웹 서비스에 문제가있을 수 있습니다. –