1.我在eclipse中创建:
在wildfly 10中部署的EJB
一个Web服务,它从创建的ejb会话bean中调用一个方法来验证登录名和密码 我有两个表Abonne(对于用户)和Compte(对于帐户)
2.随着我创建了一个Android应用程序,只要有人输入他的登录名和密码就会使用帖子请求。
应用返回:例外:null
我真的不熟悉网络服务请求,而且我没有找到关于如何创建帖子请求以及如何使用它的正确文档。
在服务器端:
1.这是我的网络服务:
@Stateless
@Path("/login")
public class RestWS {
@EJB
private ILocal metier ;
@POST
@Consumes(MediaType.APPLICATION_JSON)
public String log(String ch)
{
String retour ;
int success = metier.log(ch);
if (success ==1)
{
retour ="Success";
}
else {
retour="Fail";
}
return retour ;
}
}
2.这是我在我的网络服务中调用的方法来自我的会话Bean
@Stateless
public class EJBImplement implements ILocal
{
@PersistenceContext
EntityManager em;
public int log (String ch )
{
int exit =0;
Compte c ;
String [] parts =ch.trim().split("\\&");
String part1 =parts[0];
String part2=parts[1];
String [] KeyValue1 = part1.trim().split("\\=");
String key1=KeyValue1[0];
String Value1=KeyValue1[1];
String [] KeyValue2 = part2.trim().split("\\=");
String key2=KeyValue2[0];
String Value2=KeyValue2[1];
String login =Value1.trim();
String password=Value2.trim();
Query q = em.createQuery
("Select c from Compte c
where c.login lik:x and c.password like :x1 ");
q.setParameter("x", login);
q.setParameter("x1", password);
List<Compte>l=q.getResultList();
if (!l.isEmpty()) {
c =l.get(0);
exit=1;
}
return exit;
}
}
在我的Android应用中 1.这是我在Android应用程序中的asyntask,我使用了我的webService:
public class SendPostRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try {
URL url = new URL("http://192.168.1.6:8080/RestRegister/login");
JSONObject postDataParams = new JSONObject();
postDataParams.put("login",s1);
postDataParams.put("password", s2);
Log.e("params", postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
String chaine =getPostDataString(postDataParams);
Log.e("chaine", chaine);
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}
2.a将ASON字符串转换为此格式的方法(在Asyntask中调用):
参数1 = VAL1&安培; param2的= val2的
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
答案 0 :(得分:0)
您的SQL语句错误:
(Select c from Compte c
where c.login lik:x and c.password like :x1 ");
将“lik”替换为“like”并再试一次