Android的HttpPost登录动作 [英] Android HttpPost Login action

查看:96
本文介绍了Android的HttpPost登录动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我在做测试,如果我的应用程序可以从Android手机通过HTTP发送post.I'm我需要测试应用程序中的一些额外的参数登录,但每次我运行它,我可以登录连我的PARAMS是真实的。这里是code,我使用。

Actually I'm doing tests if my application can Login from android phone via http post.I'm sending a few extra parameters which I need for testing the application,but everytime I run it I can login even my params are true. Here is the code that I'm using.

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在code<$p$p><$c$c>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

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

我怎样才能解决这个问题,所以我可以测试登录?任何意见或建议,欢迎!
在此先感谢!

How can I fix this so I can test a login?any ideas or suggestions are welcomed! Thanks in advance!

推荐答案

要得到你应该做这样的回应:

To get the response you should do like this:

        HttpEntity entity = response.getEntity();

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

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

If you have any further questions don't hesitate to ask me.

这篇关于Android的HttpPost登录动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆