开发者

Android HttpPost Login action

开发者 https://www.devze.com 2023-03-30 20:34 出处:网络
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 e

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&开发者_如何学Pythonamp;      - 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());
            }

        }
    });

}

}

I'm setting all the params in code because as I said it's just for test.As a result after the executing http post I need to send to the server something like that :

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!


Have you checked whether you can login to your server using desktop browser or some utility like curl command line utility? Username and password hash mentioned in your comment and in code are different. You should be able to see parameters received on server with the help of server logs.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号