Android HttpPost登录操作

时间:2011-08-25 07:48:24

标签: android login http-post

实际上我正在做测试,如果我的应用程序可以通过http post从Android手机登录。我发送一些额外的参数,我需要测试应用程序,但每次我运行它我甚至可以登录我的参数是真的。这是我正在使用的代码。

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());
            }

        }
    });

}

}

我在代码中设置所有参数因为正如我所说的那样只是为了测试。在执行http发布之后,我需要向服务器发送类似的内容:

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

如何解决此问题以便我可以测试登录?欢迎提出任何想法或建议! 提前谢谢!

2 个答案:

答案 0 :(得分:0)

您是否检查过是否可以使用桌面浏览器或某些实用程序(如curl命令行实用程序)登录服务器?您的评论和代码中提到的用户名和密码哈希是不同的。您应该能够在服务器日志的帮助下查看服务器上收到的参数。

答案 1 :(得分:0)

要获得响应,您应该这样做:

        HttpEntity entity = response.getEntity();

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

如果您有任何其他问题,请不要犹豫。