开发者

Android Java - convert HttpPost result to byte[]

开发者 https://www.devze.com 2023-04-02 21:03 出处:网络
I\'m trying to get the HttpPost result from server and converting it into byte array and get the the specific bytes from byte[]. For now I\'m using this code and it\'s working. I need to change it to

I'm trying to get the HttpPost result from server and converting it into byte array and get the the specific bytes from byte[]. For now I'm using this code and it's working. I need to change it to what I want to do. Here is the code :

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( res开发者_StackOverflowponseBody )
                    .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);
                }

            }
        });
    }
}

As a result of this HttpPost I'm getting a string which I need to convert to byte as I did and get the specific bytes from it..Like to start from 15 element to 25 or something like this. Any suggestions how can I do that?

Thanks in advance for all suggestions and help.


extracting a subarray from an array is done with arrayCopy http://developer.android.com/reference/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29

for elements 15 to 25:

byte[] subarray = new byte[25-15];
System.arrayCopy(b, 15, subarray, 0, subarray.length);


Use EntityUtils.toByteArray() method instead:

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

And to get first 5 bits of array, use Arrays.copyOf() method:

byte[] fiveLengthArray = Arrays.copyOf(responseValueArray, 5);


Use

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

instead of

execute(HttpUriRequest request)

with custom responseHandler. This way you can handle input stream directly and do anything you want with it. Using EntityUtils is GC ineficient I suppose. If you care about this kind of stuff.

0

精彩评论

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