我正在尝试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 个或类似的东西。有什么建议我该怎么做?
提前感谢所有建议和帮助。