我是Android新手。我面临的问题是KitKat下面的Android版本使用Retrofit为POST请求提供了500个服务器错误,但是相同的代码适用于以后的版本,即5.1和Marshmallow。
我错过了什么?有什么建议吗?
我的界面: 公共接口LoginService {
String API_BASE_URL = "https://penna--pennadev.cs18.my.salesforce.com/";
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create());
LoginService api = builder.build().create(LoginService.class);
@FormUrlEncoded
@Headers({"Accept: application/json", "Content-Type: application/x-www-form-urlencoded"})
@POST("services/oauth2/token")
Call<DataClass> getAccessToken(
@Field("password") String password,
@Field("username") String adminName,
@Field("client_id") String client_id,
@Field("client_secret") String secret,
@Field("grant_type") String grantType);
@FormUrlEncoded
@Headers({"Accept: application/json",
"Content-Type: application/x-www-form-urlencoded"
})
@POST("services/apexrest/requestLoc")
Call<String> replaceSFDC(@Header("Authorization") String token,
@Field("IMEI") String Imei,
@Field("lat") String latitude,
@Field("long") String longitude,
@Field("gps") String gps,
@Field("connection") String connection,
@Field("netConnected") String netConnected
);
我的服务: if(isConnected == true){
Call<DataClass> call = LoginService.api.getAccessToken(".........", "...........@hotmail.com","client_key","client_secret", "grant_type");
finalCapturedData = db.getAllInfo();
call.enqueue(new Callback<DataClass>() {
@Override
public void onResponse(Call<DataClass> call, Response<DataClass> response) {
if (response.isSuccessful()) {
Toast.makeText(MyService.this, "Access Token : " + response.body().getAccess_token(), Toast.LENGTH_SHORT).show();
postDeviceState(response.body().getAccess_token());
getData();
} else {
Toast.makeText(MyService.this, "Can't get response", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<DataClass> call, Throwable t) {
}
});
}
private void postDeviceState(String access_token) {
Call<String> callReplace = LoginService.api.replaceSFDC("OAuth " + access_token,
finalCapturedData.get(1), finalCapturedData.get(2), finalCapturedData.get(3),
finalCapturedData.get(4), finalCapturedData.get(5), finalCapturedData.get(6));
callReplace.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Toast.makeText(MyService.this, "SFDC ID" + response.body(), Toast.LENGTH_SHORT).show();
Toast.makeText(MyService.this, "Data sent on server", Toast.LENGTH_SHORT).show();
Log.d("SFDC ID", response.body());
//Toast.makeText(MyService.this, "Delete " + finalCapturedData.get(0), Toast.LENGTH_SHORT).show();
Log.d("Delete : ", finalCapturedData.get(0));
db.deleteData(Integer.parseInt(finalCapturedData.get(0)));
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}