duan0417 2016-04-10 06:45
浏览 78

Android登录将带有“api_key”的JSON发送到服务器

Hello I'm a newbie develop android. I am trying to learn about login with JSON. Now my server developer partner ask for me to send a JSON request like below:

{
   "operation":"register",
   "api_key":"123",

   "user":{
        "name":"Wang Jian",
        "email":"wangjian0716@gmail.com",
        "password":"12345678"
   }
}

Now i am asked for add "api_key" to matching his server code. There are code as below Please help me to check whether it would succeed to send a JSON like above.

ServerRequest.java

public class ServerRequest {

private String operation;
private String api_key;
private User user;

public void setOperation(String operation) {
    this.operation = operation;
}

public void setUser(User user) {
    this.user = user;
}

public void setApi_key(String api_key) {
    this.api_key = "123";
}

}

RegisterFragment.java

private void registerProcess(String name, String email,String password){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RequestInterface requestInterface = retrofit.create(RequestInterface.class);

    User user = new User();
    user.setName(name);
    user.setEmail(email);
    user.setPassword(password);
    //user.setApi_key(api_key);
    ServerRequest request = new ServerRequest();
    request.setOperation(Constants.REGISTER_OPERATION);
    request.setApi_key(Constants.KEY);
    request.setUser(user);
    Call<ServerResponse> response = requestInterface.operation(request);

    response.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

            ServerResponse resp = response.body();
            Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
            progress.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {

            progress.setVisibility(View.INVISIBLE);
            Log.d(Constants.TAG, "failed");
            Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();


        }
    });
}

Constants.java

public class Constants {

public static final String BASE_URL = "http://wangjian.site90.net/";
//public static final String BASE_URL = "http://bravonet.my/ct2/api/";
public static final String REGISTER_OPERATION = "register";
public static final String KEY = "api_key";
public static final String LOGIN_OPERATION = "login";
public static final String CHANGE_PASSWORD_OPERATION = "chgPass";

public static final String SUCCESS = "success";
public static final String FAILURE = "failure";
public static final String IS_LOGGED_IN = "isLoggedIn";

public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String UNIQUE_ID = "unique_id";

public static final String TAG = "Learn2Crack";

}

The code above i just modify from learn2crack original code link to Android Login Registration System with PHP and MySQL (Updated) – Server #1

I'm a really newbie at this stuff so any help will be appreciated. thanks so much!

There are my RequestInterface.java

public interface RequestInterface {

@POST("learn2crack-login-register/")
//@POST("user/")
//@POST("android/login_registration/")
Call<ServerResponse> operation(@Body ServerRequest request);

}

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler