【问题标题】:consuming a post request(created locally) in android that return exception :null在 android 中使用返回异常的发布请求(本地创建):null
【发布时间】:2018-06-05 00:19:50
【问题描述】:

1.我在eclipse中创建:

在 Wildfly 10 中部署的 EJB

从创建的用于验证登录名和密码的 ejb 会话 bean 调用方法的 Web 服务 我有两个 Tables Abonne(对于用户)和 Compte(对于帐户)

2. 有了它,我创建了一个 android 应用程序,只要有人输入他的登录名和密码,它就会使用 post 请求。

应用返回:异常:null

我真的不熟悉 Web 服务请求,另一方面,我也没有找到关于如何创建发布请求以及如何使用它的正确文档。

在服务器端:

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.这是我在使用我的 webService 的 android 应用程序中的异步任务:

    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.一种将 JSON 字符串转换为这种格式的方法(在 Asyntask 中调用):

param1=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();
}

【问题讨论】:

    标签: android jboss7.x resteasy java-ee-7 wildfly-10


    【解决方案1】:

    您的 SQL 语句有问题:

    (Select c from Compte c 
          where c.login lik:x and c.password like :x1 ");
    

    将“喜欢”替换为“喜欢”,然后重试

    【讨论】:

    • 其实这不是问题,在复制我的代码时,我不知道like中的“e”是如何被删除的。我仍然没有找到解决我的问题的方法。谢谢。
    • 请在服务ejb中发布来自请求的调试信息。
    猜你喜欢
    • 2020-10-21
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多