android- Retrofit 1.9 - 发布数据会为LoginActivity返回无效的JSON

时间:2016-06-28 17:18:30

标签: android json post gson

我做错了与向服务器发送登录信息有关。我尝试了很多方法来访问" isSuccessful:true"事情,但我失败了。

改造错误讯息:

*D/Retrofit:*

{"IsSuccessfull":false,"IncidentId":"de3d","Messages":
[{"Key":"ExceptionResponse","Value":"SOME_ERRORS_OCCURED_DURING_EXECUTION"},>{"Key":"Exception","Value":"ServiceExecutor.InvalidApplication"}]}*

*D/Retrofit: 
<--- END HTTP (224-byte body)*

Server以这种方式获取Json:

    {
      UserName:'fatih.kaya',
      Password:'11111',    
      AuthProvider:'1'
    }

并且应该返回:

    {
      "Data": {something},
      "IsSuccessfull": true
    }
private void serviceWithHeaderTask() {
        showProgress();

        JSONObject jsonMain = new JSONObject();
        JSONObject jsonParams = new JSONObject();

        try {
            jsonParams.put("UserName", "fatih.kaya");
            jsonParams.put("Password", "11111");
            jsonParams.put("AuthProvider", "1");

            // jsonMain.put("function", "login");
            jsonMain.put("parameters", jsonParams);
        } catch (Exception e) {

        }


        /** convert sample json to retrofit typedString **/
        TypedString parm = new TypedString(jsonMain.toString());
        // service call
        serviceWithHeader.postJsonString(parm, new Callback<Object>() {


            @Override
            public void success(Object object, Response response) {

                String responseString = new Gson().toJson(object);
                showSnackBar(responseString);

                hideProgress();
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                retrofitError.printStackTrace();

                showSnackBar("Error:"+retrofitError.getMessage());
                hideProgress();
            }
        });
    }

***RestInterface***

    public interface RestInterface {

        @POST("/auth/signin")
        void postJsonString(@Body TypedString string, Callback<Object> cb);
    }


***ServiceGenerator***

    public class ServiceGenerator {

        public static final String BASE_URL = "http://xxx.xxx.xxx.xxx:xxx/api/json/";

        public static class SessionRequestInterceptor implements RequestInterceptor {

        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Content-Type", "application/json");
            request.addHeader("x-Application-Key", "PIRLWjnl");
            }
        }


        public static <S> S createServiceWithJsonHeader(Class<S> serviceClass) {
            RestAdapter.Builder builder = new RestAdapter.Builder()
                .setEndpoint(BASE_URL)
                .setRequestInterceptor(new SessionRequestInterceptor());

            RestAdapter adapter = builder.build();
            adapter.setLogLevel(RestAdapter.LogLevel.FULL);
            return adapter.create(serviceClass);
    }

3 个答案:

答案 0 :(得分:0)

使用Gson和jsonschema2pojo.org而不是手动解析Json

更方便

答案 1 :(得分:0)

Retrofit(2.0之前)附带(我认为是Gson)作为默认的JSON Parser,所以你唯一应该改变的是:

serviceWithHeader.postJsonString(parm, new Callback<LoginResponse>() { //This is the key ingredient
/* implementation */
}

然后你应该创建LoginReponse类

class LoginResponse {
    Object Data;
    Boolean IsSuccessfull;
}

创建这些类并在回调中设置它后,Type Retrofit应该注意填充这些字段。 如果您不想通过json响应创建这类类,我已经创建了一个您可能觉得有用的小脚本。

https://github.com/Pato94/JsonToJavaConverter

答案 2 :(得分:0)

我改变了这部分

TypedString parm = new TypedString(jsonMain.toString());

用这个:

TypedString parm = new TypedString(jsonParams.toString());

它有效。因此无需添加parameters字。

感谢那些试图帮助的人=)