1

我正在尝试HttpPost从服务器获取结果并将其转换为字节数组并从中获取特定字节byte[]。现在我正在使用这段代码并且它正在工作。我需要将其更改为我想做的事情。这是代码:

public class UserLogin extends Activity {

    Button cancel,login;
    HttpClient httpclient;
    HttpPost httppost;
    ArrayList<NameValuePair> postParameters;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://www.rpc.test.com");

        postParameters = new ArrayList<NameValuePair>();
        //postParameters.add(new BasicNameValuePair("new_auth_data","1"));
        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);
                    Log.d("Login", "Response " + response.getEntity());

                    String responseBody = EntityUtils.toString(response.getEntity());
                    byte[] b = responseBody.getBytes();
                    // here




                    new AlertDialog.Builder( UserLogin.this )
                    .setTitle( "Warning" )
                    .setMessage( responseBody )
                    .setPositiveButton( "Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Log.d("AlertDialog", "Positive");
                        }
                    })
                    .show();

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

            }
        });
    }
}

作为这个 HttpPost 的结果,我得到了一个字符串,我需要像我一样将其转换为字节并从中获取特定的字节......就像从 15 个元素开始到 25 个或类似的东西。有什么建议我该怎么做?

提前感谢所有建议和帮助。

4

3 回答 3

1

从数组中提取子数组是使用 arrayCopy http://developer.android.com/reference/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int ,%20int%29

对于元素 15 到 25:

byte[] subarray = new byte[25-15];
System.arrayCopy(b, 15, subarray, 0, subarray.length);
于 2011-09-07T08:47:39.343 回答
1

利用

execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler)

代替

execute(HttpUriRequest request)

使用自定义 responseHandler。这样你就可以直接处理输入流并用它做任何你想做的事情。我想使用 EntityUtils 是 GC 效率低下的。如果你关心这种东西。

于 2011-09-07T08:53:49.813 回答
1

改用EntityUtils.toByteArray()方法:

byte[] responseValueArray = EntityUtils.toByteArray(response.getEntity());

要获取数组的前 5 位,请使用Arrays.copyOf()以下方法:

byte[] fiveLengthArray = Arrays.copyOf(responseValueArray, 5);
于 2011-09-07T08:37:50.703 回答