2013-07-19 150 views
0

請幫助我米使用此代碼匹配url值的用戶名和密碼登錄認證

和Java代碼,

public class AndroidHTTPRequestsActivity extends Activity implements OnClickListener { 

    private EditText usernameEditText; 
    private EditText passwordEditText; 
    private Button sendGetReqButton; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     usernameEditText = (EditText) findViewById(R.id.main_username_editText); 
     passwordEditText = (EditText) findViewById(R.id.main_password_editText); 

     sendGetReqButton = (Button) findViewById(R.id.main_sendGetReq_button); 
     sendGetReqButton.setOnClickListener(this); 
    } 

    public void onClick(View v) { 

     if(v.getId() == R.id.main_sendGetReq_button) { 
      // Get the values given in EditText fields 
      String givenUsername = usernameEditText.getText().toString(); 
      String givenPassword = passwordEditText.getText().toString(); 
      System.out.println("Given usernames is :" + givenUsername + " Given password is :" + givenPassword); 

      // Pass those values to connectWithHttpGet() method 
      connectWithHttpGet(givenUsername, givenPassword); 
     }   
    } 

    private void connectWithHttpGet(String givenUsername, String givenPassword) { 

     class HttpGetAsyncTask extends AsyncTask<String, Void, String> { 

      @Override 
      protected String doInBackground(String... params) { 

       String email = params[0]; 
       String pwd = params[1]; 
       System.out.println("email is :" + email + " pwd is :" + pwd); 


       HttpClient httpClient = new DefaultHttpClient(); 


       HttpGet httpGet = new HttpGet("http://ipadress/folder/filename.php?command=CMD_LOGIN&email=name&pwd=00"); 

       try { 
        HttpResponse httpResponse = httpClient.execute(httpGet); 
        System.out.println("httpResponse"); 
        // that comes as the httpResponse 
        InputStream inputStream = httpResponse.getEntity().getContent(); 

        InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
        StringBuilder stringBuilder = new StringBuilder(); 
        String bufferedStrChunk = null; 

        while((bufferedStrChunk = bufferedReader.readLine()) != null){ 
         stringBuilder.append(bufferedStrChunk); 
        } 
        System.out.println("Returning value of doInBackground :" + stringBuilder.toString()); 

        return stringBuilder.toString(); 
       } catch (ClientProtocolException cpe) { 
        System.out.println("Exception generates caz of httpResponse :" + cpe); 
        cpe.printStackTrace(); 
       } catch (IOException ioe) { 
        System.out.println("Second exception generates caz of httpResponse :" + ioe); 
        ioe.printStackTrace(); 
       } 

       return null; 
      } 

      @Override 
      protected void onPostExecute(String result) { 
       super.onPostExecute(result); 

       if(result.equals("1")) 
       { 

        Toast.makeText(getApplicationContext(), "HTTP GET is working...", Toast.LENGTH_LONG).show(); 
       }else{ 
        Toast.makeText(getApplicationContext(), "Invalid...", Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 

     HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask(); 

     httpGetAsyncTask.execute(givenUsername, givenPassword); 
    } 
} 

後。其中認證並獲得響應logcat中創建登錄頁面喜歡這個。如何將結果值相匹配登錄Sucess

if(result.equal("")) 

表明我應該使用結果比較?我嘗試了value==1,它不起作用。

07-19 13:54:26.509: I/System.out(13951): Given usernames is :name Given password is :00 
07-19 13:54:26.519: I/System.out(13951): email is :name pwd is :00 
07-19 13:54:27.689: I/System.out(13951): httpResponse 
07-19 13:54:27.689: I/System.out(13951): Returning value of doInBackground :<?xml version="1.0" encoding="ISO-8859-1"?><command><value>1</value><email>[email protected]</email><id>100001</id><first_name>Sue</first_name><last_name>Stappler</last_name><photo>images/teacher.png</photo><total_students>2</total_students><student_list> <student>  <id>200001</id>  <first_name>John</first_name>  <last_name>Mathew</last_name>  <photo>images/student.png</photo> </student> <student>  <id>200002</id>  <first_name>Steve</first_name>  <last_name>Jobs</last_name>  <photo>images/student.png</photo> </student></student_list><description_title>Login Success</description_title><description_message>Correct username and password</description_message><session_id>ecaf065b3213179afcd0d9895691505c</session_id></command> 

回答

0

你可進行檢查這一要求的兩種狀態

  • HTTP響應代碼
    • 檢查數據是否與'HTTP返回200 OK'
  • 數據檢查
    • 反序列化響應(SimpleXML,Jackson Json或FasterXML pro JECT)
    • 檢查反序列化狀態(如果模型改變或響應是空的,它可能會失敗)在反序列化對象
    • 匹配字段到你的條件