我是java中的Android和Web服务的新手。我试图将Java对象或类作为参数传递给Web服务中的方法。我使用Android应用程序作为客户端,当我使用字符串时,一切正常但现在我想传递一个包含斜杠的字符串变量' /'所以它似乎没有起作用。所以我认为最好的选择是传入一个Object作为参数,这样我就能从Web服务中获取它们的任何格式的值。请问如何编写服务器端和客户端代码。 这是我以前的网络服务代码
@Path("/Student")
public class StudentController {
StudentService studentService;
@GET
@Path("/{username},{password}")
@Produces(MediaType.APPLICATION_JSON)
public Student authenticate(@PathParam("username") String username, @PathParam("password") String password) {
StringTokenizer st = new StringTokenizer(username, "/");
String school = st.nextToken();
studentService = new StudentService("Laurels");
return studentService.validateStudent("Laurels/2015/"+username, "Laurels/2015/"+password);
}
}
这是我之前的Android客户端代码
private class Authenticate extends AsyncTask<Void, Void, Void>{
Snackbar snackbar = Snackbar.make(btnLogin, "Result", Snackbar.LENGTH_LONG);
private int status;
private Bundle bundle;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
String url = "http://192.168.42.209:8085/SchoolWebService/rest/Student/"+username+","+password;
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject c = new JSONObject(jsonStr);
String uname = c.getString("username");
String pword = c.getString("password");
String surname = c.getString("surname");
boolean validated = c.getBoolean("validated");
if(!validated){
snackbar.setText("Invalid Login").show();
status = 0;
}else{
snackbar.setDuration(Snackbar.LENGTH_SHORT).setText("Authenticated!").show();
status = 1;
bundle = new Bundle();
bundle.putString("username", username);
bundle.putString("password", password);
bundle.putBoolean("validated", validated);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
snackbar.setText("Connection Failed!").show();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing()) {
pDialog.dismiss();
}
if(status == 1){
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
}
}