在 PHP 中,$id 变量应该从客户端接收一个整数值。但是,当客户端使用POST请求方法将id值作为整数发送到PHP脚本时,当脚本上传到WAMP服务器时,它会自动转换为PHP中的字符串类型。另一方面,当脚本上传到Bluehost服务器上时,id值仍然是整数,不会转换为字符串,这正是我想要的。
这是一个简单的 PHP 脚本:
<?php $id = $_POST["id"]; if (is_int($id)) echo "It is integer"; // It will print this if the PHP script was uploaded to the Bluehost server. else if (is_string($id)) echo "It is string"; // It will print this if the PHP script was uploaded to the WAMP server.
从客户端发送的 id 值是通过 Android 应用程序发送的,这就是我将 id 值发送到 PHP 脚本的方式:
RetrofitManager 类
public class RetrofitManager { private static RetrofitManager.Api api; public static RetrofitManager.Api getApi() { if (api == null) api = new Retrofit.Builder() .baseUrl("http://192.151.5.721/API/") .client(new OkHttpClient.Builder().readTimeout(3, TimeUnit.MINUTES).writeTimeout(3, TimeUnit.MINUTES).connectTimeout(25, TimeUnit.SECONDS).build()) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) .build() .create(Api.class); return api; } public interface Api { @FormUrlEncoded @POST("Countries/Get.php") Single<CountryModelResponse> getCountries(@Field("id") int countryId); } }
CountriesRepository 类
public class CountriesRepository { public LiveData<Object> getCountries(Context context) { MutableLiveData<Object> mutableLiveData = new MutableLiveData<>(); PublishSubject<String> retrySubject = PublishSubject.create(); RetrofitManager.getApi().getCountries(Navigation.findNavController(MainActivity.activityMainBinding.activityMainFragmentContainerViewContainer).getPreviousBackStackEntry() == null ? SharedPreferencesManager.getIntegerValue(context, SharedPreferencesManager.Keys.COUNTRY_ID.name()) : -1) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnSubscribe(mutableLiveData::setValue) .doOnError(throwable -> { LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>(); linkedHashSet.add(!Utils.isInternetConnected(context) ? 100 : throwable instanceof SocketTimeoutException ? 200 : 300); linkedHashSet.add(retrySubject); mutableLiveData.setValue(linkedHashSet); }) .retryWhen(throwableFlowable -> throwableFlowable.flatMap(throwable -> retrySubject.toFlowable(BackpressureStrategy.DROP).take(1), (throwable, s) -> s)) .subscribe(countryModelResponse -> { if (countryModelResponse.getRequestStatus() == 100) mutableLiveData.setValue(countryModelResponse); else { LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>(); linkedHashSet.add(300); linkedHashSet.add(retrySubject); mutableLiveData.setValue(linkedHashSet); } }); return mutableLiveData; } }
我不确定为什么两台服务器之间会出现这种行为差异。
我正在使用最新版本的 PHP 和 WAMP 服务器。