我使用Retrofit2向服务器发送10个值。我发送的值应该是(根据记录):
getRecommendations 值:
document.body.style.color = 'black';
document.body.style['background-color'] = 'white';
但是,当我通过改造将值发送到服务器(到PHP脚本)时,它们将作为
发送
-1, -1, -1, -1, -1, -1, 3, 25, 44.182345, 7.1627383.
。
我知道这是因为我在Android Studio中使用了PHP拦截器。以下是PHP拦截器显示的内容:
-1, -1 ,0 ,-1 ,-1 ,0 ,25 ,44.182345 , 7.1627383
我很困惑为什么会这样 - 有些值只是随机改变了,下面我附上了我的代码部分。
maxGradientLo=-1&maxGradientHi=-1&totalDistanceLo=0&totalDistanceHi=-1&altitudeGainLo=-1&altitudeGainHi=-1&numberToRecommend=0&searchWithin=25¤tLatitude=44.182345¤tLongitude=7.1627349853515625
private List<RecommendationJsonResponse> jsonResponse = null;
private void getRecommendations(int maxGradientLo, int maxGradientHi, int totalDistanceLo, int totalDistanceHi, int altitudeGainLo, int altitudeGainHi, int numberToRecommend, int searchWithin, double currentLatitude, double currentLongitude){
Log.d(Constants.SkiCompanionDebug, "getRecommendations values: " + maxGradientLo + ", "+ maxGradientHi + ", "+ totalDistanceLo + ", "+ totalDistanceHi + ", "+ altitudeGainLo + ", "+ altitudeGainHi + ", "+ numberToRecommend + ", "+ searchWithin);
RetrofitApiInterface retrofitApiInterface = RetrofitApiClient.getApiClient().create(RetrofitApiInterface.class);
Call<List<RecommendationJsonResponse>> callRecommendations = retrofitApiInterface.downloadRecommendations(maxGradientLo, maxGradientHi, totalDistanceLo, totalDistanceHi, altitudeGainLo, altitudeGainHi, numberToRecommend, searchWithin, currentLatitude, currentLongitude);
callRecommendations.enqueue(new Callback<List<RecommendationJsonResponse>>() {
@Override
public void onResponse(Call<List<RecommendationJsonResponse>> call, Response<List<RecommendationJsonResponse>> response) {
jsonResponse = response.body();
//calling helper functions to populate map with ski tracks
List<List<GeoPoint>> trackList = new ArrayList<>();
trackList = populateGeoPointLists();
populateMap(trackList);
}
@Override
public void onFailure(Call<List<RecommendationJsonResponse>> call, Throwable t) {
Log.d(Constants.SkiCompanionDebug, "onFailure: downloadRecommendations: " + t.getMessage());
}
});
}
@POST("download_recommendations.php")
@FormUrlEncoded
Call<List<RecommendationJsonResponse>> downloadRecommendations(@Field("maxGradientLo") int maxGradientLo,
@Field("maxGradientHi") int maxGradientHi,
@Field("totalDistanceLo") int totalDistanceLo,
@Field("totalDistanceHi") int totalDistanceHi,
@Field("altitudeGainLo") int altitudeGainLo,
@Field("altitudeGainHi") int altitudeGainHi,
@Field("numberToRecommend") int numberToRecommend,
@Field("searchWithin") int searchWithin,
@Field("currentLatitude") double currentLatitude,
@Field("currentLongitude") double currentLongitude);
final int[] gradient = new int[2];
final int[] distance = new int[2];
final int[] altitude = new int[2];
final int[] recommendNumber = new int[1];
final int[] searchWithin = new int[1];
//initializing variables
gradient[0] = -1;
gradient[1] = -1;
distance[0] = -1;
distance[1] = -1;
altitude[0] = -1;
altitude[1] = -1;
recommendNumber[0] = 3;
searchWithin[0] = 25;
getRecommendations(gradient[0], gradient[1], distance[0], distance[1], altitude[0], altitude[1], recommendNumber[0], searchWithin[0], 44.182345, 7.1627383);
$maxGradientLo = $_POST["maxGradientLo"]; //degrees
$maxGradientHi = $_POST["maxGradientHi"];
$totalDistanceLo = $_POST["totalDistanceLo"]; //kilometers
$totalDistanceHi = $_POST["totalDistanceHi"];
$altitudeGainLo = $_POST["altitudeGainLo"]; //meters
$altitudeGainHi = $_POST["altitudeGainHi"];
$numberToRecommend = $_POST["numberToRecommend"];
$searchWithin = $_POST["searchWithin"]; //number of kilometers track can be from user
$currentLatitude = $_POST["currentLatitude"];
$currentLongitude = $_POST["currentLongitude"];
// echo "gradLo: " .$maxGradientLo ." gradHi: " .$maxGradientHi." distLo: " .$totalDistanceLo." distHi: " .$totalDistanceHi." altLo: " .$altitudeGainLo." altHi: " .$altitudeGainHi. " numTorecommend: ".$numberToRecommend;
02-22 17:12:31.967 25165-25426/skicompanion.skicompanion D/OkHttp: --> POST http://192.168.1.11/ski_companion/download_recommendations.php
02-22 17:12:31.967 25165-25426/skicompanion.skicompanion D/OkHttp: Content-Type: application/x-www-form-urlencoded
02-22 17:12:31.970 25165-25426/skicompanion.skicompanion D/OkHttp: Content-Length: 204
02-22 17:12:31.971 25165-25426/skicompanion.skicompanion D/OkHttp: maxGradientLo=-1&maxGradientHi=-1&totalDistanceLo=0&totalDistanceHi=-1&altitudeGainLo=-1&altitudeGainHi=-1&numberToRecommend=0&searchWithin=25¤tLatitude=44.182345¤tLongitude=7.1627349853515625
02-22 17:12:31.972 25165-25426/skicompanion.skicompanion D/OkHttp: --> END POST (204-byte body)
02-22 17:12:32.212 25165-25426/skicompanion.skicompanion D/OkHttp: <-- 200 OK http://192.168.1.11/ski_companion/download_recommendations.php (239ms)
02-22 17:12:32.212 25165-25426/skicompanion.skicompanion D/OkHttp: Date: Thu, 22 Feb 2018 17:12:33 GMT
02-22 17:12:32.212 25165-25426/skicompanion.skicompanion D/OkHttp: Server: Apache/2.4.29 (Win32) OpenSSL/1.1.0g PHP/7.2.1
02-22 17:12:32.213 25165-25426/skicompanion.skicompanion D/OkHttp: X-Powered-By: PHP/7.2.1
02-22 17:12:32.213 25165-25426/skicompanion.skicompanion D/OkHttp: Keep-Alive: timeout=5, max=100
02-22 17:12:32.213 25165-25426/skicompanion.skicompanion D/OkHttp: Connection: Keep-Alive
02-22 17:12:32.213 25165-25426/skicompanion.skicompanion D/OkHttp: Transfer-Encoding: chunked
02-22 17:12:32.213 25165-25426/skicompanion.skicompanion D/OkHttp: Content-Type: text/html; charset=UTF-8
02-22 17:12:32.405 25165-25426/skicompanion.skicompanion D/OkHttp: [{"track_id":0,"latitude":"44.1831217","longitude":"7.161585"},{"track_id":0,"latitude":"44.183055","longitude":"7.161585"},{"track_id":0,"latitude":"44.183045","longitude":"7.1616433"},{"track_id":0,"latitude":"44.1830083","longitude":"7.16175"},{"track_id":0,"latitude":"44.1829467","longitude":"7.1618833"},{"track_id":0,"latitude":"44.1828983","longitude":"7.1619783"},{"track_id":0,"latitude":"44.18284","longitude":"7.1620783"},{"track_id":0,"latitude":"44.1827917","longitude":"7.1621867"},{"track_id":0,"latitude":"44.1826983","longitude":"7.1621983"},{"track_id":0,"latitude":"44.182595","longitude":"7.1622067"},{"track_id":0,"latitude":"44.1825133","longitude":"7.16229"},{"track_id":0,"latitude":"44.1824533","longitude":"7.1623667"},{"track_id":0,"latitude":"44.182395","longitude":"7.16242"},{"track_id":0,"latitude":"44.1823717","longitude":"7.1623967"},{"track_id":0,"latitude":"44.1823517","longitude":"7.1624283"},{"track_id":0,"latitude":"44.1822717","longitude":"7.162435"},{"track_id":0,"latitude":"44.1821583","longitude":"7.16242"},{"track_id":0,"latitude":"44.1820867","longitude":"7.1624083"},{"track_id":0,"latitude":"44.1820217","longitude":"7.162385"},{"track_id":0,"latitude":"44.182065","longitude":"7.1624167"},{"track_id":0,"latitude":"44.18215","longitude":"7.1624717"},{"track_id":0,"latitude":"44.1822383","longitude":"7.16253"},{"track_id":0,"latitude":"44.18233","longitude":"7.1626317"},{"track_id":0,"latitude":"44.182345","longitude":"7.1627383"},{"track_id":0,"latitude":"44.18233","longitude":"7.1628467"},{"track_id":0,"latitude":"44.1823267","longitude":"7.1629483"},{"track_id":0,"latitude":"44.1823267","longitude":"7.1630267"},{"track_id":0,"latitude":"44.1823117","longitude":"7.163115"},{"track_id":0,"latitude":"44.1822867","longitude":"7.163195"},{"track_id":0,"latitude":"44.1822633","longitude":"7.1633067"},{"track_id":0,"latitude":"44.18226","longitude":"7.163425"},{"track_id":0,"latitude":"44.1822883","longitude":"7.1635117"},{"track_id":0,"latitude":"44.1823217","longitude":"7.163615"},{"track_id":0,"latitude":"44.18236","longitude":"7.1637"},{"track_id":0,"latitude":"44.1823733","longitude":"7.163795"},{"track_id":0,"latitude":"44.182385","longitude":"7.1639"},{"track_id":0,"latitude":"44.1823867","longitude":"7.1640017"},{"track_id":0,"latitude":"44.1823767","longitude":"7.1641033"},{"track_id":0,"latitude":"44.1823733","longitude":"7.1641967"},{"track_id":0,"latitude":"44.1823567","longitude":"7.164295"},{"track_id":0,"latitude":"44.1823367","longitude":"7.16443"},{"track_id":0,"latitude":"44.1823433","longitude":"7.164535"},{"track_id":0,"latitude":"44.18232","longitude":"7.1646767"},{"track_id":0,"latitude":"44.18232","longitude":"7.1647667"},{"track_id":0,"latitude":"44.1823133","longitude":"7.1648767"},{"track_id":0,"latitude":"44.182315","longitude":"7.16499"},{"track_id":0,"latitude":"44.1823067","longitude":"7.1650817"},{"track_id":0,"latitude":"44.1822983","longitude":"7.165165"},{"track_id":0,"latitude":"44.1822983","longitude":"7.1652583"},{"track_id":0,"latitude":"44.1823083","longitude":"7.1653317"},{"track_id":0,"latitude":"44.1823067","longitude":"7.1654133"},{"track_id":0,"latitude":"44.182305","longitude":"7.165505"},{"track_id":0,"latitude":"44.1823067","longitude":"7.16558"},{"track_id":0,"latitude":"44.1822833","longitude":"7.1656767"},{"track_id":0,"latitude":"44.1822733","longitude":"7.165755"},{"track_id":0,"latitude":"44.1822667","longitude":"7.16584"},{"track_id":0,"latitude":"44.182255","longitude":"7.1659233"},{"track_id":0,"latitude":"44.182245","longitude":"7.1659967"},{"track_id":0,"latitude":"44.18225","longitude":"7.166075"},{"track_id":0,"latitude":"44.182245","longitude":"7.16616"},{"track_id":0,"latitude":"44.1822333","longitude":"7.1662483"},{"track_id":0,"latitude":"44.1822383","longitude":"7.1663117"},{"track_id":0,"latitude":"44.1822267","longitude":"7.16642"},{"track_id":0,"latitude":"44.1822117","longitude":"7.1665033"},{"track_id":0,"latitude":"44.1822033","l
02-22 17:12:32.405 25165-25426/skicompanion.skicompanion D/OkHttp: ongitude":"7.16657"},{"track_id":0,"latitude":"44.1821867","longitude":"7.1666433"},{"track_id":0,"latitude":"44.182185","longitude":"7.166705"},{"track_id":0,"latitude":"44.182165","longitude":"7.1668067"},{"track_id":0,"latitude":"44.1821467","longitude":"7.1668367"},{"track_id":0,"latitude":"44.1821417","longitude":"7.166825"},{"track_id":0,"latitude":"44.1821483","longitude":"7.1668017"},{"track_id":0,"latitude":"44.1821517","longitude":"7.1667867"},{"track_id":0,"latitude":"44.1821467","longitude":"7.166755"},{"track_id":0,"latitude":"44.1821567","longitude":"7.1668"},{"track_id":0,"latitude":"44.18216","longitude":"7.1668733"},{"track_id":0,"latitude":"44.1821567","longitude":"7.1669633"},{"track_id":0,"latitude":"44.1821517","longitude":"7.1670583"},{"track_id":0,"latitude":"44.1821483","longitude":"7.1671467"},{"track_id":0,"latitude":"44.1821433","longitude":"7.167245"},{"track_id":0,"latitude":"44.1821317","longitude":"7.167325"},{"track_id":0,"latitude":"44.182115","longitude":"7.1674017"},{"track_id":0,"latitude":"44.18211","longitude":"7.1674533"},{"track_id":0,"latitude":"44.1821033","longitude":"7.16752"},{"track_id":0,"latitude":"44.1821117","longitude":"7.1675633"},{"track_id":0,"latitude":"44.1821183","longitude":"7.16762"},{"track_id":0,"latitude":"44.1821183","longitude":"7.1676817"},{"track_id":0,"latitude":"44.18212","longitude":"7.1677"},{"track_id":0,"latitude":"44.182115","longitude":"7.167735"},{"track_id":0,"latitude":"44.1820967","longitude":"7.1677817"},{"track_id":0,"latitude":"44.1821","longitude":"7.1678133"},{"track_id":0,"latitude":"44.182095","longitude":"7.16784"},{"track_id":0,"latitude":"44.1820867","longitude":"7.1678933"},{"track_id":0,"latitude":"44.1820833","longitude":"7.1679383"},{"track_id":0,"latitude":"44.1820833","longitude":"7.167995"},{"track_id":0,"latitude":"44.1820833","longitude":"7.168045"},{"track_id":0,"latitude":"44.1820783","longitude":"7.1680867"},{"track_id":0,"latitude":"44.182085","longitude":"7.1681433"},{"track_id":0,"latitude":"44.1820933","longitude":"7.1681967"},{"track_id":0,"latitude":"44.1820767","longitude":"7.1682517"},{"track_id":0,"latitude":"44.182085","longitude":"7.16827"},{"track_id":0,"latitude":"44.1821267","longitude":"7.1683067"},{"track_id":0,"latitude":"44.1821683","longitude":"7.1684317"},{"track_id":0,"latitude":"44.1822033","longitude":"7.1685883"},{"track_id":0,"latitude":"44.1822317","longitude":"7.1687067"},{"track_id":0,"latitude":"44.1822617","longitude":"7.16882"},{"track_id":0,"latitude":"44.1822967","longitude":"7.168905"},{"track_id":0,"latitude":"44.18234","longitude":"7.168985"},{"track_id":0,"latitude":"44.1823783","longitude":"7.169075"},{"track_id":0,"latitude":"44.18241","longitude":"7.1691783"},{"track_id":0,"latitude":"44.1824467","longitude":"7.1692817"},{"track_id":0,"latitude":"44.1824733","longitude":"7.1693617"},{"track_id":0,"latitude":"44.1824983","longitude":"7.1694433"},{"track_id":0,"latitude":"44.182535","longitude":"7.1695167"},{"track_id":0,"latitude":"44.18257","longitude":"7.1695967"},{"track_id":0,"latitude":"44.1826017","longitude":"7.169675"},{"track_id":0,"latitude":"44.1826317","longitude":"7.16976"},{"track_id":0,"latitude":"44.1826617","longitude":"7.169845"},{"track_id":0,"latitude":"44.1826917","longitude":"7.1699417"},{"track_id":0,"latitude":"44.18272","longitude":"7.1700417"},{"track_id":0,"latitude":"44.18275","longitude":"7.1701333"},{"track_id":0,"latitude":"44.1827817","longitude":"7.1701833"},{"track_id":1,"latitude":"44.1927117","longitude":"7.1673133"},{"track_id":1,"latitude":"44.1927133","longitude":"7.167285"},{"track_id":1,"latitude":"44.1926983","longitude":"7.1672833"},{"track_id":1,"latitude":"44.1926833","longitude":"7.167265"},{"track_id":1,"latitude":"44.192675","longitude":"7.1672533"},{"track_id":1,"latitude":"44.19266","longitude":"7.16723"},{"track_id":1,"latitude":"44.19264","longitude":"7.1671783"},{"track_id":1,"latitude":"44.19262","longitude":"7.1671083"},{"track_id":1,"l
02-22 17:12:32.406 25165-25426/skicompanion.skicompanion D/OkHttp: atitude":"44.19258","longitude":"7.1670317"},{"track_id":1,"latitude":"44.1925267","longitude":"7.1669217"},{"track_id":1,"latitude":"44.1924933","longitude":"7.1668217"},{"track_id":1,"latitude":"44.1924533","longitude":"7.1667733"},{"track_id":1,"latitude":"44.192395","longitude":"7.1666333"},{"track_id":1,"latitude":"44.1923267","longitude":"7.16649"},{"track_id":1,"latitude":"44.19227","longitude":"7.16636"},{"track_id":1,"latitude":"44.1922167","longitude":"7.16627"},{"track_id":1,"latitude":"44.1921283","longitude":"7.1662083"},{"track_id":1,"latitude":"44.1919817","longitude":"7.16614"},{"track_id":1,"latitude":"44.1918667","longitude":"7.166115"},{"track_id":1,"latitude":"44.1917483","longitude":"7.166105"},{"track_id":1,"latitude":"44.1916233","longitude":"7.1661183"},{"track_id":1,"latitude":"44.1914917","longitude":"7.16613"},{"track_id":1,"latitude":"44.1913883","longitude":"7.1661533"},{"track_id":1,"latitude":"44.1912767","longitude":"7.166145"},{"track_id":1,"latitude":"44.19114","longitude":"7.166065"},{"track_id":1,"latitude":"44.1910617","longitude":"7.1660383"},{"track_id":1,"latitude":"44.19095","longitude":"7.166"},{"track_id":1,"latitude":"44.19086","longitude":"7.165965"},{"track_id":1,"latitude":"44.1907633","longitude":"7.1659317"},{"track_id":1,"latitude":"44.1906383","longitude":"7.1658633"},{"track_id":1,"latitude":"44.190525","longitude":"7.1658017"},{"track_id":1,"latitude":"44.1904033","longitude":"7.1657517"},{"track_id":1,"latitude":"44.1902967","longitude":"7.1657383"},{"track_id":1,"latitude":"44.19019","longitude":"7.16574"},{"track_id":1,"latitude":"44.19007","longitude":"7.1657517"},{"track_id":1,"latitude":"44.1899483","longitude":"7.1657633"},{"track_id":1,"latitude":"44.18984","longitude":"7.165785"},{"track_id":1,"latitude":"44.18971","longitude":"7.165795"},{"track_id":1,"latitude":"44.1895983","longitude":"7.16581"},{"track_id":1,"latitude":"44.18949","longitude":"7.16582"},{"track_id":1,"latitude":"44.1893817","longitude":"7.16583"},{"track_id":1,"latitude":"44.189275","longitude":"7.1658283"},{"track_id":1,"latitude":"44.189175","longitude":"7.16582"},{"track_id":1,"latitude":"44.18906","longitude":"7.1657833"},{"track_id":1,"latitude":"44.1889533","longitude":"7.1657517"},{"track_id":1,"latitude":"44.18884","longitude":"7.1656967"},{"track_id":1,"latitude":"44.18875","longitude":"7.1656267"},{"track_id":1,"latitude":"44.18863","longitude":"7.1656033"},{"track_id":1,"latitude":"44.188515","longitude":"7.1655667"},{"track_id":1,"latitude":"44.1884033","longitude":"7.1655133"},{"track_id":1,"latitude":"44.1883083","longitude":"7.1654633"},{"track_id":1,"latitude":"44.1882117","longitude":"7.1654167"},{"track_id":1,"latitude":"44.1881117","longitude":"7.1653717"},{"track_id":1,"latitude":"44.1879983","longitude":"7.1653217"},{"track_id":1,"latitude":"44.1878983","longitude":"7.1652917"},{"track_id":1,"latitude":"44.1878017","longitude":"7.16528"},{"track_id":1,"latitude":"44.18771","longitude":"7.1652767"},{"track_id":1,"latitude":"44.1876183","longitude":"7.165275"},{"track_id":1,"latitude":"44.18752","longitude":"7.1652733"},{"track_id":1,"latitude":"44.1874083","longitude":"7.16527"},{"track_id":1,"latitude":"44.1873183","longitude":"7.1652817"},{"track_id":1,"latitude":"44.1872233","longitude":"7.1653033"},{"track_id":1,"latitude":"44.1871317","longitude":"7.1653467"},{"track_id":1,"latitude":"44.1870383","longitude":"7.1653917"},{"track_id":1,"latitude":"44.1869517","longitude":"7.1654417"},{"track_id":1,"latitude":"44.1868583","longitude":"7.165495"},{"track_id":1,"latitude":"44.18677","longitude":"7.1655633"},{"track_id":1,"latitude":"44.18669","longitude":"7.1656383"},{"track_id":1,"latitude":"44.186605","longitude":"7.16573"},{"track_id":1,"latitude":"44.186535","longitude":"7.1658217"},{"track_id":1,"latitude":"44.186465","longitude":"7.1659117"},{"track_id":1,"latitude":"44.1863983","longitude":"7.1659867"},{"track_id":1,"latitude":"44.186335","longitude":"7.1661067"},{"tra
02-22 17:12:32.406 25165-25426/skicompanion.skicompanion D/OkHttp: ck_id":1,"latitude":"44.186295","longitude":"7.16623"},{"track_id":1,"latitude":"44.1862533","longitude":"7.1663767"},{"track_id":1,"latitude":"44.1862033","longitude":"7.166505"},{"track_id":1,"latitude":"44.1861433","longitude":"7.1666783"},{"track_id":1,"latitude":"44.1860867","longitude":"7.1668217"},{"track_id":1,"latitude":"44.1860267","longitude":"7.1669667"},{"track_id":1,"latitude":"44.1859567","longitude":"7.167095"},{"track_id":1,"latitude":"44.1858883","longitude":"7.167195"},{"track_id":1,"latitude":"44.1857967","longitude":"7.1673233"},{"track_id":1,"latitude":"44.1857317","longitude":"7.1674467"},{"track_id":1,"latitude":"44.1856667","longitude":"7.16758"},{"track_id":1,"latitude":"44.1855983","longitude":"7.16769"},{"track_id":1,"latitude":"44.185535","longitude":"7.1678017"},{"track_id":1,"latitude":"44.1854533","longitude":"7.1679117"},{"track_id":1,"latitude":"44.1853717","longitude":"7.1680217"},{"track_id":1,"latitude":"44.18529","longitude":"7.1681383"},{"track_id":1,"latitude":"44.1852317","longitude":"7.1682567"},{"track_id":1,"latitude":"44.1851583","longitude":"7.1683567"},{"track_id":1,"latitude":"44.1850833","longitude":"7.1684633"},{"track_id":1,"latitude":"44.1850283","longitude":"7.1685667"},{"track_id":1,"latitude":"44.18496","longitude":"7.168655"},{"track_id":1,"latitude":"44.1848783","longitude":"7.1687267"},{"track_id":1,"latitude":"44.1847933","longitude":"7.1687967"},{"track_id":1,"latitude":"44.1847017","longitude":"7.168855"},{"track_id":1,"latitude":"44.1846083","longitude":"7.1689367"},{"track_id":1,"latitude":"44.1845067","longitude":"7.16901"},{"track_id":1,"latitude":"44.1843967","longitude":"7.1690867"},{"track_id":1,"latitude":"44.1843","longitude":"7.1691717"},{"track_id":1,"latitude":"44.1842217","longitude":"7.1692883"},{"track_id":1,"latitude":"44.1841533","longitude":"7.1694117"},{"track_id":1,"latitude":"44.184075","longitude":"7.1695017"},{"track_id":1,"latitude":"44.1839783","longitude":"7.1695567"},{"track_id":1,"latitude":"44.1838567","longitude":"7.16957"},{"track_id":1,"latitude":"44.1837533","longitude":"7.16956"},{"track_id":1,"latitude":"44.1836517","longitude":"7.1695017"},{"track_id":1,"latitude":"44.1835567","longitude":"7.1694333"},{"track_id":1,"latitude":"44.1834767","longitude":"7.1693583"},{"track_id":1,"latitude":"44.1833983","longitude":"7.1692867"},{"track_id":1,"latitude":"44.1833317","longitude":"7.1692033"},{"track_id":1,"latitude":"44.1832567","longitude":"7.169105"},{"track_id":1,"latitude":"44.1831833","longitude":"7.16896"},{"track_id":1,"latitude":"44.1831233","longitude":"7.1688117"},{"track_id":1,"latitude":"44.1830717","longitude":"7.1686533"},{"track_id":1,"latitude":"44.1830383","longitude":"7.168485"},{"track_id":1,"latitude":"44.183005","longitude":"7.1683067"},{"track_id":1,"latitude":"44.1829817","longitude":"7.1681317"},{"track_id":1,"latitude":"44.1829467","longitude":"7.1679567"},{"track_id":1,"latitude":"44.1829083","longitude":"7.1677867"},{"track_id":1,"latitude":"44.1828867","longitude":"7.16763"},{"track_id":1,"latitude":"44.1828517","longitude":"7.16745"},{"track_id":1,"latitude":"44.1828333","longitude":"7.167305"},{"track_id":1,"latitude":"44.1828083","longitude":"7.16715"},{"track_id":1,"latitude":"44.182785","longitude":"7.1669833"},{"track_id":1,"latitude":"44.1827533","longitude":"7.1667767"},{"track_id":1,"latitude":"44.182725","longitude":"7.1665817"},{"track_id":1,"latitude":"44.1827067","longitude":"7.1664417"},{"track_id":1,"latitude":"44.1826933","longitude":"7.1662883"},{"track_id":1,"latitude":"44.1826817","longitude":"7.1660983"},{"track_id":1,"latitude":"44.182675","longitude":"7.1659433"},{"track_id":1,"latitude":"44.182675","longitude":"7.165785"},{"track_id":1,"latitude":"44.1826867","longitude":"7.165625"},{"track_id":1,"latitude":"44.1827083","longitude":"7.16543"},{"track_id":1,"latitude":"44.1827233","longitude":"7.1652367"},{"track_id":1,"latitude":"44.1827233","longitude":"7.1650733"},{"track_id":1,"latit
02-22 17:12:32.406 25165-25426/skicompanion.skicompanion D/OkHttp: ude":"44.1827367","longitude":"7.1649183"},{"track_id":1,"latitude":"44.182755","longitude":"7.1647617"},{"track_id":1,"latitude":"44.1827617","longitude":"7.164645"},{"track_id":1,"latitude":"44.18278","longitude":"7.16449"},{"track_id":1,"latitude":"44.1827783","longitude":"7.1643717"},{"track_id":1,"latitude":"44.1827717","longitude":"7.1642483"},{"track_id":1,"latitude":"44.1827733","longitude":"7.1640983"},{"track_id":1,"latitude":"44.18277","longitude":"7.1639367"},{"track_id":1,"latitude":"44.1827733","longitude":"7.163795"},{"track_id":1,"latitude":"44.1827633","longitude":"7.1636433"},{"track_id":1,"latitude":"44.1827533","longitude":"7.1634967"},{"track_id":1,"latitude":"44.18275","longitude":"7.16339"},{"track_id":1,"latitude":"44.1827517","longitude":"7.1632383"},{"track_id":1,"latitude":"44.1827583","longitude":"7.16308"},{"track_id":1,"latitude":"44.182775","longitude":"7.1628883"},{"track_id":1,"latitude":"44.18279","longitude":"7.1627317"},{"track_id":1,"latitude":"44.1827933","longitude":"7.162585"},{"track_id":1,"latitude":"44.1828067","longitude":"7.1624017"},{"track_id":1,"latitude":"44.1827967","longitude":"7.1622367"},{"track_id":1,"latitude":"44.1828483","longitude":"7.1621017"},{"track_id":1,"latitude":"44.18292","longitude":"7.16194"}]
02-22 17:12:32.406 25165-25426/skicompanion.skicompanion D/OkHttp: <-- END HTTP (17280-byte body)
答案 0 :(得分:1)
这是一个非常恼人的问题,我不完全理解这个解决方案。如果有人能解释为什么我不能静态调用这个方法 - 我会非常感激。它告诉我如何调用RetrofitApiClient:我静态调用了getApiClient(),但我通过创建一个新的RetrofitApiClient对象然后在其上调用getApiClient()来修复它。解决方案如下:
旧破代码:
RetrofitApiInterface retrofitApiInterface = RetrofitApiClient.getApiClient().create(RetrofitApiInterface.class);
新工作代码:
RetrofitApiClient retrofitApiClient = RetrofitApiClient.getInstance();
RetrofitApiInterface retrofitApiInterface = retrofitApiClient.getApiClient().create(RetrofitApiInterface.class);
private static Retrofit retrofit = null;
public static RetrofitApiClient instance = null;
public static RetrofitApiClient getInstance(){
if(instance == null){
instance = new RetrofitApiClient();
}
return instance;
}
答案 1 :(得分:1)
为了实现Retrofit实例的单例设计模式,请参阅以下代码:
public class ApiClient extends Application {
private static ApiClient apiClient = null; //singleton Object
String apiURL = "http://your.url/";
public static ApiClient getApiClient() {//getter for object
if (apiClient == null)
apiClient = new ApiClient();
return apiClient;
}
@Override
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
public Retrofit getClient() {//Retrofit instance
int myTime = 15;
OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder()
.connectTimeout(myTime, TimeUnit.SECONDS)
.readTimeout(myTime, TimeUnit.SECONDS)
.writeTimeout(myTime, TimeUnit.SECONDS)
.addNetworkInterceptor(new StethoInterceptor());
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(apiURL)
.client(okHttpBuilder.build())
.addConverterFactory(GsonConverterFactory.create());
return builder.build();
}
}
现在使用改造请参阅以下代码:
LoginDTO loginDTO = new LoginDTO(
etPhno.getText().toString().trim(),
etPass.getText().toString().trim()
);
ApiService service = ApiClient.getApiClient().getClient().create(ApiService.class);
Call<LoginResp> call = service.loginAPI(loginDTO);
call.enqueue(new Callback<LoginResp>() {
@Override
public void onResponse(Call<LoginResp> call, Response<LoginResp> response) {
LoginResp loginResp = response.body();
}
@Override
public void onFailure(Call<LoginResp> call, Throwable t) {
Toast.makeText(Login.this, "Error here", Toast.LENGTH_SHORT).show();
}
});
在ApiService接口中声明如下函数:
@POST("login")
Call<LoginResp> loginAPI(@Body LoginDTO loginDTO);
LoginDTO类是这样的:
public class LoginDTO {
@SerializedName("log_phno")
@Expose
private String user;
@SerializedName("log_pass")
@Expose
private String pass;
public LoginDTO(String user, String pass) {
this.user = user;
this.pass = pass;
}
public String getUser() {
return user;
}
public String getPass() {
return pass;
}
}
现在最重要的部分,转到清单文件,在<application>
标记下,将您的单例类名称前面加一个点(。),如下所示:
android:name=".Controller.ApiClient"
了解Stetho Interceptor的魔力,运行你的应用并打开谷歌浏览器并输入chrome://inspect/