2012-11-15 2 views
0

나는 안드로이드 응용 프로그램에 초보자입니다. 이미 웹 사이트에서 등록 된 사용자 만 로그인 할 수있는 로그인 페이지를 만들었습니다. 사용자가 자신의 사용자 이름과 암호를 입력하면 등록 된 사용자를 인증하려고합니다. 사용자 이름과 암호를 인증하려면 어떻게해야합니까? 나는 다음 코드를 사용하여 이것을보고 도와 줘. 로그인 버튼을 클릭하면 오류가 표시됩니다.로그인 웹 사이트에서 등록 된 사용자에 대해서만 인증

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 

답변

1

이 예를보십시오.

http post method passing null values to the server

은 여기에서 웹 서버에 사용자 이름 & 암호를 보낼 수 있고 당신은 요청에 따라 서버에서 응답을받을 것입니다. 그래서 사용 reg 또는 서비스 측면에서 확인하셔야합니다.

응답을 검색하려면 JSON을 사용해야합니다.

+0

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

+0

위의 예는 앱 – SRY

+0

앱을 닫고 데이터가 웹으로 전송되는지 확인합니다. 그렇지 않으면 웹 서비스에 문제가있을 수 있습니다. –

0

실제로 logcat을 올바르게 확인하면 달성하고자하는 것을 달성했음을 알 수 있습니다.

시도한 내용 : 로그인을 위해 HTTP POST 요청 보내기. 응답 내용 : HTML 페이지.

webapp는 특정 HTTP POST 요청에 대해 true/false를 반환하지 않습니다. 반면에 사실을 확인하는 반면.

솔루션 :
1. 구문 분석 HTML 페이지 (NOT 권장) 성공적으로 로그인
2.뿐만 아니라 XML/JSON 응답을 반환하는 당신에게 웹 응용 프로그램을 재 설계하기위한 결정. 웹 페이지와 다른 앱에서 사용할 수있는 API를 둘 다 가질 수 있습니다.

다른 조치 사항 :
1. UI 스레드에서 네트워크 호출을하지 마십시오. 결국 얻을 수도 있습니다 ANR
2. TAG없이 Log 문을 두지 마십시오. 태그를 사용하면 디버깅이 쉬워집니다. 거대한 덩어리의 코드를 작성하고이 코드보다 복잡한 문제를 다룰 때 이것을 이해할 수 있습니다.

희망이 도움이됩니다.

관련 문제