Android版的Htt prequest问题 [英] Android HttpRequest issue

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

问题描述

我使用 HttpPost 的Htt prequest 来连接到服务器,并得到结果。我得到的结果作为字符串,并试图将其转换成字节[] 。但是,当我这样做,这我越来越为字符串字节[] 的结果是不同的。我该如何解决呢?
这里是code我使用的是:

Hi I'm using HttpPost and HttpRequest to connect to a server and get the result. I get the result as string and trying to convert it in byte[]. But when I do that the result which I'm getting as String and byte[] is different. How can I fix that? Here is the code I'm using :

public class TestProjectActivity extends Activity {

    Button cancel,login;
    HttpClient httpclient;
    HttpPost httppost;
    ArrayList<NameValuePair> postParameters;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://www.rpc.example.com");

        postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("username_hash", "c34a6cf6bff9f6b61e96fdf4bf360157d522a17c"));
        postParameters.add(new BasicNameValuePair("password_hash", "56dc55f0062cf21797637b0f8652293023f2ef22"));

        cancel = (Button) findViewById(R.id.cancel_login_btn);
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        login = (Button) findViewById(R.id.login_btn);
        login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

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

                    HttpResponse response = httpclient.execute(httppost);
                    String responseBody = EntityUtils.toString(response.getEntity()); //response
                    byte[] b = responseBody.getBytes();
                    Log.e("Packet","Response packet : "+b); //print packet
                    Log.e("Packet","Response packet : "+responseBody); //print packet





                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("ERROR"," Error lol - "+e);
                }

            }
        });
    }
}

结果作为字符串是:响应数据包: 00000000000000000000000000000001002001ec8ce6abb3e952a85b8551ba726a122700000000000000000000000000000002000000000000000000000000000001162c1c0624f7a2b272e05c4d997473a8e6001eyJsb2NhbGUiOiJlbl9VUyIsImlkIjoyLCJlcnJvcl9jb2RlIjo2MDAzLCJlcnJvcl9zdHJpbmciOiJVbnN1cHBvcnRlZCBDbGllbnQgVmVyc2lvbiJ9

结果为字节[]为:应答报文:[B @ 462d93c0

推荐答案

字节[]。的toString()始终打印[B @。 ..

使用 Log.e(数据包,包回应:+新的String(二,UTF-8)); //打印包

这将创建一个新的字符串从您字节[] (你并不真正需要做的,因为你有它已经从您所呼叫的其他方法)。

This will create a new String from you byte[] (which you don't really need to do, since you have it already from the other method you are calling).

这篇关于Android版的Htt prequest问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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