我试图在Android App和Java Servlet之间创建一个简单的Web服务。我从servlet返回的响应是JSONObject,但我在Android应用程序中收到的是布尔值。给出了servlet和android的代码。
ANDROID CODE
class ExecuteTask extends AsyncTask<Void, Void, Void> {
String param;
@Override
protected Void doInBackground(Void... params) {
try {
param = "param1=" + URLEncoder.encode(_username, "UTF-8") + "¶m2=" + URLEncoder.encode(_password, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
URL url = null;
try {
url = new URL(url_login);
} catch (Exception e) {
e.printStackTrace();
}
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
conn.setDoOutput(true);
conn.setFixedLengthStreamingMode(param.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter out = null;
try {
out = new PrintWriter(conn.getOutputStream());
out.print(param);
Log.d("Checking Params", param);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
String response = "";
Scanner inStream = null;
// Log.d("JSON", json.toString());
try {
int responseCode = conn.getResponseCode();
if(responseCode ==200){
// conn.setDoInput(true);
inStream = new Scanner(conn.getInputStream());
}else{
InputStream in = null;
in = conn.getErrorStream();
}
}catch (IOException e) {
e.printStackTrace();
}
while (inStream.hasNextLine()) {
response += (inStream.hasNextLine());
try {
json = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
try {
String s = json.getString("Login");
Log.d("MSG", s);
if (s != null) {
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Log.d("Msg sent", s);
finish();
} else if (s.equals("fail")) {
Toast.makeText(context, "Unable to load the schedule", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
conn.disconnect();
}
return null;
}
}
RUNQUERY METHOD
public JSONObject RunQuery(String[] params, HttpServletRequest request, HttpServletResponse response) {
// System.out.println("The parameters are: " + params[0] + params[1] +
// params[2]);
String sql = "SELECT * FROM job_recommender.user where User_Name='"+ params[0] +"' AND password='"+ params[1] +"'";
// System.out.println("Our SQL Statement is " + sql);
JSONObject json = new JSONObject();
// JSONArray jArray = null;
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(sql);
if (rs.next()) {
System.out.println("This is RS" + rs);
JSONObject jObj = new JSONObject();
jObj.put("username", (new String(rs.getString("User_Name"))));
jObj.put("password", (new String(rs.getString("password"))));
System.out.println(jObj);
json.put("Login", jObj);
System.out.println(json.put("Login", jObj));
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("This is our JSON" + json);
response.setContentType("Content-Type=application/json");
response.setCharacterEncoding("charset=UTF-8");
PrintWriter out;
try {
System.out.println("response JSON" + json.toString());
out = response.getWriter();
out.println(json);
//response.getWriter().write(json.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
SERVLET
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.setContentType("text/html");
Enumeration paramNames = request.getParameterNames();
//System.out.println(paramNames);
String params[] = new String[2];
int i = 0;
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
System.out.println("Checking ParamNames" + paramName);
String[] paramValues = request.getParameterValues(paramName);
params[i] = paramValues[0];
ulogin.setUsername(params[0]);
ulogin.setPassword(params[1]);
// System.out.println(params[i]);
i++;
}
String name = ulogin.getUsername();
String password = ulogin.getPassword();
System.out.println("username" + name + "password" + password);
WebServiceDAO wdao = new WebServiceDAO(getServletContext());
wdao.RunQuery(params, request, response);
response.getWriter().append("Served at: ").append(request.getContextPath());
}
谁能告诉我我在这里做错了什么?我已经尝试过各种方法来解决这个问题。任何帮助对我都有用。
答案 0 :(得分:2)
您的代码中存在拼写错误
response += (inStream.hasNextLine());
应该是
response += (inStream.nextLine());