0

我正在尝试从移动应用程序登录我的网络应用程序,但出现服务器错误 (500),并且 ruby​​ 端的参数为空。我觉得这很奇怪,因为我可以使用相同的过程很好地注册,即以 JSON 格式发送数据并在控制器中运行创建操作。

在 ruby​​ 应用程序接收 JSON 并填充参数的位置之间我是否缺少一个步骤?

这是控制器:

def create
    user = User.find_by_email(params[:app_session][:email].downcase)
    if user && user.authenticate(params[:app_session][:password])
      respond_with do|format|
        format.json { render json: user }
      end
    else
      respond_with do|format|
        format.json { render json: { :info => "Failed" }}
      end
    end
end

这是java代码:(我知道密码是公开的,这将得到解决)

@Override
        protected Object doInBackground(Void... params) {
            try {

                URL url = new URL(myurl);

                //json.put("success", true);
                json.put("email", mUserEmail);
                json.put("password", mUserPassword);

                HttpClient httpclient = new DefaultHttpClient();
                HttpParams myParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(myParams, 10000);
                HttpConnectionParams.setSoTimeout(myParams, 10000);

                try {
                    HttpPost httppost = new HttpPost(url.toString());
                    httppost.setHeader("Accept", "application/json");
                    httppost.setHeader("Content-type", "application/json");

                    StringEntity se = new StringEntity(json.toString());
                    httppost.setEntity(se);
                    HttpResponse response = httpclient.execute(httppost);

                    String temp = EntityUtils.toString(response.getEntity());
                    jsonResponse = new JSONObject(temp);

                    value = true;
                } catch (ClientProtocolException e) {

                } catch (IOException e) {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } finally {

            }
            return value;
        }

编辑:这里是 Heroku 日志:

Started GET "/app_session" for 62.40.34.218 at 2013-05-20 19:14:01 +0000
Processing by AppSessionController#create as JSON
Parameters: {"app_session"=>{}}
Completed 200 OK in 2ms (Views: 0.2ms | ActiveRecord: 1.0ms)
4

1 回答 1

0

我已经在 Android 中实现了一些代码,它看起来像这样:

        HttpPost post = new HttpPost(login_url);
        post.setHeader("Content-type", "application/json");
        post.addHeader("Accept", "application/json");

        JSONObject send_json = new JSONObject();
        send_json.put("login", uname);
        send_json.put("password", pwd);
        JSONObject root = new JSONObject();
        root.put("app_session", send_json);
        StringEntity se = new StringEntity(root.toString());


        Log.i("send_json", send_json.toString());
        post.setEntity(se);

        HttpResponse response = httpclient.execute(post);

它看起来只是设置标题的区别。当您使用 setHeader() 时,我使用了 addHeader()。虽然我不是 android 专家,但上面的代码对我有用。

希望这可以帮助!!

于 2013-05-20T15:08:12.713 回答