2011-08-25 38 views
1

其實我正在做測試,如果我的應用程序可以通過http post.Am發送一些額外的參數,我需要測試應用程序,但我每次運行它我甚至可以登錄我的參數是真實的。這是我正在使用的代碼。Android HttpPost登錄動作

new_auth_data=1&debug_data=1&client_api_ver=1.5.1.422&timestamp=1314195661&password_hash=d2824b50d07cfed3d82a480d5d87437af11a4f7e&set_locale=en_US&device_os_type=iPhone%20Simulator%204.3.2&username_hash=6d229fe6593f5250653e3b29184a6e370fc7ffe5&device_sync_type=3&device_identification_string=iPhone%20Simulator%204.3.2&device_resolution=320x480&device_identificator=255634997 

package com.android.login.test; 

/* new_auth_data=1&  - If there is new user ot password changes 
* debug_data=1&  - Debugging data sent from terminal 
* client_api_ver=1.5.1.422& - API version of clients terminal 
* timestamp=1314195661&  - THE timestamp of first sync of new device 
* password_hash=d2824b50d07cfed3d82a480d5d87437af11a4f7e&  - A valid password hash 
* set_locale=en_US&  - 
* device_os_type=iPhone%20Simulator%204.3.2&  - The device OS code - for mobiles 
* username_hash=6d229fe6593f5250653e3b29184a6e370fc7ffe5&  - A valid username hash 
* device_sync_type=3&  - 3 is for iphone, 4 will be for android 
* device_identification_string=iPhone%20Simulator%204.3.2&  - Device identificator (friendly name) 
* device_resolution=320x480&  - No need of explanation 
* device_identificator=255634997  - This identificator is catched with getDeviceId() function 

*/ 
import java.util.ArrayList; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class LoginActivity extends Activity { 

     EditText username,password; 
Button login; 
TextView error; 
ArrayList<NameValuePair> postParameters; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    username = (EditText)findViewById(R.id.username); 
    password = (EditText) findViewById(R.id.password); 
    error = (TextView) findViewById(R.id.error); 
    final HttpClient httpclient = new DefaultHttpClient(); 
    final HttpPost httppost = new HttpPost("http://www.rpc.example.com"); 


    postParameters = new ArrayList<NameValuePair>(); 
    postParameters.add(new BasicNameValuePair("username_hash", username.getText().toString())); 
    postParameters.add(new BasicNameValuePair("password_hash", password.getText().toString())); 
    /*postParameters.add(new BasicNameValuePair("debug_data","1")); 
    postParameters.add(new BasicNameValuePair("client_api_ver","1.5.1.422")); 
    postParameters.add(new BasicNameValuePair("timestamp","1314195661")); 
    postParameters.add(new BasicNameValuePair("set_locale","en_US")); 
    postParameters.add(new BasicNameValuePair("device_os_type","Android 2.2")); 
    postParameters.add(new BasicNameValuePair("device_sync_type","3")); 
    postParameters.add(new BasicNameValuePair("device_identificator","255634997")); 
    postParameters.add(new BasicNameValuePair("device_identification_string",deviceId));*/ 
    login = (Button) findViewById(R.id.login); 
    login.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      try { 
       httppost.setEntity(new UrlEncodedFormEntity(postParameters)); 

       HttpResponse response = httpclient.execute(httppost); 
       Log.d("myapp", "response " + response.getEntity()); 

       String responseBody = EntityUtils.toString(response.getEntity()); 

       error.setText(responseBody); 
      } catch (Exception e) { 
       username.setText(e.toString()); 
      } 

     } 
    }); 

} 

} 

因爲我說的只是test.As結果執行HTTP POST後,我需要向服務器發送類似的東西我設置的所有PARAMS代碼

我該如何解決這個問題,以便我可以測試登錄?歡迎任何想法或建議! 在此先感謝!

+0

我不確定我是否理解現在正在發生的事情......你是否說它總是會說「正確的用戶名或密碼」,即使它不正確?你能告訴我們服務器正在接收什麼? –

+0

它總是說用戶名和密碼是不正確的。我怎樣才能看到服務器正在從我的應用程序或服務器接收...(抱歉,這是我第一次與服務器和用戶登錄過程同步)。 –

回答

0

您是否檢查過您是否可以使用桌面瀏覽器或curl命令行實用程序等實用程序登錄到您的服務器?您在評論和代碼中提到的用戶名和密碼散列有所不同。您應該能夠在服務器日誌的幫助下查看服務器上收到的參數。

+0

其實我檢查過,我可以用瀏覽器的哈希用戶名和密碼登錄到服務器。我只是直接把它們作爲字符串發佈,而不是生成它們,看看我是否可以用這種方式登錄。關於不同的哈希...我知道,他們是來自iPhone模擬器的測試,所以他們只是一個示例,以顯示我應該發佈到服務器。而且,當我發佈params到服務器後,我得到一個數據包,如果發送給我登錄或錯誤。 –

+0

那麼,在這種情況下,當你使用上面的代碼登錄時,你將不得不檢查服務器端收到的內容。 「UrlEncodedFormEntity」類使用「US-ASCII」編碼作爲默認編碼,有時會導致問題。嘗試將編碼設置爲UTF-8。不知道服務器端發生了什麼,很難確定原因。 – Nilesh

+0

實際上,改變後的代碼工作正常..我無法登錄,但至少我得到了服務器的響應,它發送給我一個數據包,現在我必須找到如何從中獲取數據。現在問題更加不同。無論如何感謝幫助!我真的很感激它:) –

0

要獲得你應該做這樣的迴應:

 HttpEntity entity = response.getEntity(); 

     String html = null; 
     if (entity != null) { 
      InputStream instream = entity.getContent(); 
      try { 
       html = streamToString(instream); 
      } finally { 
       instream.close(); 
      } 
     } 

如果您有任何問題請不要猶豫,問我。