【问题标题】:Get return value of method from api explorer endpoint android从 api explorer 端点 android 获取方法的返回值
【发布时间】:2014-09-10 11:18:38
【问题描述】:

我想返回我从我的 android 端点调用的方法。

这是我在 google explorer api 上测试实体 NewUsers 中是否已有用户的方法(当我在 explorer api 上测试时,该方法工作正常):

/**
 * Method to update the information about the user.
 * We don't allow to update the email.
 * @param : String email (to compare), String sexe, String age, String weight,
 * @return : JSONObject containing the response
 */
@ApiMethod(name = "greetings.userExist", httpMethod = "get", path = "jsonobject/userExist")
public JSONObject userExist(@Named("Email") String email) {

    JSONObject obj = new JSONObject();
    obj.put("Value", "false");  

    Query q = new Query ("NewUsers");
    q.addFilter("Email", Query.FilterOperator.EQUAL, email);

    PreparedQuery pq = datastore.prepare(q);        

    for (Entity entity : pq.asIterable ())
    {
        // = user already exist
        if(entity.getProperty("Email").equals(email)){
            obj.put("Value", "true");       
        }       
    }
    return obj;
}

这是来自我的客户端端点 android (EDITED) 的代码:

// Get the jsonObject to know if the user already exist
    userExist = new AsyncTask<String, Void, JSONObject>() {

        JSONObject jsonObject;

        @Override
        protected JSONObject doInBackground(String... params) {

            // Retrieve service handle.
            Helloworld apiServiceHandle = AppConstants.getApiServiceHandle();

            try {
                // Call the api method and pass the value
                return (JSONObject) apiServiceHandle.greetings().userExist(emailUser).getJsonContent();

            } catch (IOException e) {
                Log.e("Error", e.toString());
            }

            return null;
        }

        @Override
        protected void onPostExecute(JSONObject obj) {
            Log.i("Value :",obj.toString());
            if(obj != null){
                try {
                    value = obj.getString("Value");
                    Log.i("Value :", value);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

    };

    userExist.execute();

当我打印结果为空..

我怎样才能得到所有的jsonObject?

有什么想法吗?谢谢

【问题讨论】:

    标签: android json google-app-engine android-asynctask google-cloud-endpoints


    【解决方案1】:

    我建议你创建一个单独的类,而不是创建一个 JsonObject ,它包含所有必需的字段。确保这个类是 Serializable 并放输出中的对象。 您将找到响应的对象。

    【讨论】:

      猜你喜欢
      • 2020-11-27
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      相关资源
      最近更新 更多