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

配列からサブ配列を抽出するには、arrayCopyhttp://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

EntityUtils.toByteArray()代わりにメソッドを使用してください:

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

そして、配列の最初の5ビットを取得するには、次のArrays.copyOf()メソッドを使用します。

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

使用する

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

それ以外の

execute(HttpUriRequest request)

カスタムresponseHandlerを使用します。このようにして、入力ストリームを直接処理し、それを使ってやりたいことが何でもできます。EntityUtilsの使用はGC非効率的だと思います。あなたがこの種のものを気にするなら。

于 2011-09-07T08:53:49.813 に答える