到目前为止,我认为我能做的是,在Web服务中,返回信息为byte [],所以我有一个问题的ArrayList,这是我制作的一个有5字节[]的类,如下所示:
public class Question {
private byte[] question;
private byte[] correctAnswer;
private byte[] answer1;
private byte[] answer2;
private byte[] answer3;
public Question(){
}
public void setQuestion(byte[] question) {
this.question = question;
}
public void setCorrectAnswer(byte[] correctAnswer) {
this.correctAnswer = correctAnswer;
}
public void setAnswer1(byte[] answer1) {
this.answer1 = answer1;
}
public void setAnswer2(byte[] answer2) {
this.answer2 = answer2;
}
public void setAnswer3(byte[] answer3) {
this.answer3 = answer3;
}
public byte[] getQuestion() {
return question;
}
public byte[] getCorrectAnswer() {
return correctAnswer;
}
public byte[] getAnswer1() {
return answer1;
}
public byte[] getAnswer2() {
return answer2;
}
public byte[] getAnswer3() {
return answer3;
}
}
返回arraylist的Webservice方法:
public ArrayList<Question> GetQuestions() {
ArrayList<Question> questions = new ArrayList();
try {
connection = DriverManager.getConnection("jdbc:mysql://" + DBIP + "/" + DBName + "?user=" + DBUser + "&password=" + DBPassword);
String query
= "SELECT * FROM questions;";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Question question = new Question();
question.setQuestion(rs.getString("question").getBytes());
question.setCorrectAnswer(rs.getString("correctAnswer").getBytes());
question.setAnswer1(rs.getString("answer1").getBytes());
question.setAnswer2(rs.getString("answer2").getBytes());
question.setAnswer3(rs.getString("answer3").getBytes());
questions.add(question);
}
stmt.close();
connection.close();
} catch (SQLException ex) {
System.out.println(ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(DataBaseManager.class.getName()).log(Level.SEVERE, null, ex);
}
return questions;
}
获取SOAP响应的Android应用程序方法:
SoapObject request = new SoapObject( NAMESPACE, METHOD_NAME );
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11 );
envelope.setOutputSoapObject( request );
HttpTransportSE androidHttpTransport = new HttpTransportSE( URL );
androidHttpTransport.call( SOAP_ACTION, envelope );
SoapObject response = ( SoapObject ) envelope.getResponse();
for ( int i = 0; i < response.getPropertyCount(); i++ ) {
Question question = new Question();
question.setQuestion(response.getProperty( "question" ).toString());
question.setAnswer1(response.getProperty( "question" ).toString());
question.setAnswer2(response.getProperty( "question" ).toString());
question.setAnswer3(response.getProperty( "question" ).toString());
question.setCorrectAnswer(response.getProperty( "question" ).toString());
questions.add( question );
}
考虑到这一点,我想在我的Android应用程序上接收这个ArrayList并将字节解码为那里的字符串,但我遇到的问题是我得到的SOAP响应,我只能将它作为一个字符串,所以我得到一个表示字节的字符串,我无法解码,因为它已经是字符串形式
有没有办法将String解码为字符串?