1:先看一看xutils3的提交代码
String account = editText1.getText().toString(); String password = editText2.getText().toString(); JSONObject js_request = new JSONObject();//服务器需要传参的json对象 try { js_request.put("account", account);//添加相应键值对 js_request.put("password", password); } catch (JSONException e) { e.printStackTrace(); } RequestParams requestParams = new RequestParams(LOGIN_URL); requestParams.setAsJsonContent(true); requestParams.setBodyContent(js_request.toString()); x.http().post(requestParams, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { System.out.println("**ok"+result); try { JSONObject object = new JSONObject(result); String code = object.getString("code"); if (code.equals("1")) { // button.setClickable(false); //登录成功后获得id } else { // 登陆失败 } } catch (Exception e) { e.printStackTrace(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { System.out.println("errot"); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); }
2:Retrofit提交过程
2.1 登陆 urL
public static String LOGIN_URL = "http://114.xx.xxx.xx:8088/vdyweb/ws/rest/Login";
interface APIStore { @Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头 @POST ("vdyweb/ws/rest/Login") Call<ResponseBody>getMessage(@Body RequestBody info); // 请求体味RequestBody 类型
}
public class Info { String account; String password; public Info(String account, String password) { this.account = account; this.password = password; } }
public class MainActivity extends AppCompatActivity { public static String BASE_LOGIN_URL = "http://114.xx.xxx.xx:8088/"; Retrofit retrofit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Info info=new Info("test","123456"); /*** 利用Gson 将对象转json字符串*/ Gson gson=new Gson(); String obj=gson.toJson(info); retrofit=new Retrofit.Builder().baseUrl(BASE_LOGIN_URL).build(); RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),obj); final APIStore login = retrofit.create(APIStore.class); retrofit2.Call<ResponseBody> data = login.getMessage(body); data.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(retrofit2.Call<ResponseBody> call, Response<ResponseBody> response) { Log.d(TAG, "onResponse: --ok--"+response.body()); try { Log.d(TAG, "onResponse: --ok--"+response.body().string()); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) { Log.d(TAG, "onResponse: --err--"+t.toString()); } });} }
3:添加get请求
apiStore加
@GET("vdyweb/ws/rest/device/getOwnerDevice/2/2/20") Call<ResponseBody>getMessage2();
retrofit2.Call<ResponseBody>data1=login.getMessage2(); data1.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try { Log.d(TAG, "onResponse: --ok--"+response.body().string()); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { } });
时间: 2024-11-22 22:30:23