Retrofit2与Android Studio:无法获取加油站数组

huangapple go评论97阅读模式
英文:

Retrofit2 with Android Studio: Cant get the array of petrol-stations

问题

Here's the translated code you provided:

package com.example.benzinpreise;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.*;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TextView textViewResult;
    private List<Tankstellen.Stations> tankstellenList = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewResult = findViewById(R.id.text_view_result);
        getTankstellen();
    }

    private void getTankstellen() {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

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

        TankstellenApi tankstellenApi = retrofit.create(TankstellenApi.class);
        Call<Tankstellen> call = tankstellenApi.getTankstellen("57.001", "10.007", "7", "all", "dist", TankstellenApi.API_KEY);
        call.enqueue(new Callback<Tankstellen>() {
            @Override
            public void onResponse(Call<Tankstellen> call, Response<Tankstellen> response) {

                String displayResponse = "";
                String displayStations = "";
                Tankstellen antwort = response.body();
                tankstellenList = antwort.stationsList;

                displayResponse += "ok: " + antwort.isOk() + "\n" + "license: " + antwort.getLicense() + "\n" + "data: " + antwort.getData() + "\n" + "status: " + antwort.getStatus() + "\n";

                for (Tankstellen.Stations stationen : tankstellenList) {
                    displayStations += stationen.id + " "
                            + stationen.name + " "
                            + stationen.brand + " "
                            + stationen.street + " "
                            + stationen.place + " "
                            + stationen.lat + " "
                            + stationen.lng + " "
                            + stationen.dist + " "
                            + stationen.diesel + " "
                            + stationen.e5 + " "
                            + stationen.e10 + " "
                            + stationen.isOpen + " "
                            + stationen.houseNumber + " "
                            + stationen.postCode + "\n";
                }
                textViewResult.setText(displayResponse + "\n" + displayStations);
            }

            @Override
            public void onFailure(Call<Tankstellen> call, Throwable t) {
                textViewResult.setText(t.getMessage());
            }
        });
    }
}
package com.example.benzinpreise;

import com.google.gson.annotations.SerializedName;
import java.util.List;

public class Tankstellen {
    private boolean ok;
    private String license;
    private String data;
    private String status;
    
    @SerializedName("stations")
    public List<Stations> stationsList = null;

    public class Stations {
        @SerializedName("id")
        public String id;
        public String name;
        public String brand;
        public String street;
        public String place;
        public double lat;
        public double lng;
        public double dist;
        public double diesel;
        public double e5;
        public double e10;
        public boolean isOpen;
        public String houseNumber;
        public int postCode;
    }

    public boolean isOk() {
        return ok;
    }

    public String getLicense() {
        return license;
    }

    public String getData() {
        return data;
    }

    public String getStatus() {
        return status;
    }
}
package com.example.benzinpreise;

import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface TankstellenApi {

    String BASE_URL = "https://creativecommons.tankerkoenig.de/";
    String API_KEY = "50d25698-e799-22df-c47c-833b7ae62b8b";

    @GET("json/list.php")
    Call<Tankstellen> getTankstellen(
            @Query("lat") String lat,
            @Query("lng") String lng,
            @Query("rad") String umkreis,
            @Query("type") String type,
            @Query("sort") String filter,
            @Query("apikey") String apikey);
}

The problem you're facing might be related to the fact that you are setting the response text in the textViewResult only outside the for loop that iterates through the tankstellenList. I've adjusted the code to concatenate both the response and station details, which should display the complete response including the station information.

英文:

Api response:

{
&quot;ok&quot;: true,
&quot;license&quot;: &quot;CC BY 4.0 -  https:\/\/creativecommons.tankerkoenig.de&quot;,
&quot;data&quot;: &quot;MTS-K&quot;,
&quot;status&quot;: &quot;ok&quot;,
&quot;stations&quot;: [
{
&quot;id&quot;: &quot;35885891-164a-4cc7-a6c9-3c6f7f4e6845&quot;,
&quot;name&quot;: &quot;Andreas Linke&quot;,
&quot;brand&quot;: &quot;Nordoel&quot;,
&quot;street&quot;: &quot;Peiner Str.&quot;,
&quot;place&quot;: &quot;Sehnde&quot;,
&quot;lat&quot;: 52.31519,
&quot;lng&quot;: 9.972986,
&quot;dist&quot;: 2.3,
&quot;diesel&quot;: 1.109,
&quot;e5&quot;: 1.279,
&quot;e10&quot;: 1.239,
&quot;isOpen&quot;: true,
&quot;houseNumber&quot;: &quot;52&quot;,
&quot;postCode&quot;: 31319
},
{
&quot;id&quot;: &quot;d4cb468e-86b0-4aa0-a862-5677d22cbab1&quot;,
&quot;name&quot;: &quot;ACCESS Tankstelle Sehnde&quot;,
&quot;brand&quot;: &quot;Access&quot;,
&quot;street&quot;: &quot;LEHRTER STR. 20&quot;,
&quot;place&quot;: &quot;SEHNDE&quot;,
&quot;lat&quot;: 52.317781,
&quot;lng&quot;: 9.966101,
&quot;dist&quot;: 2.8,
&quot;diesel&quot;: 1.109,
&quot;e5&quot;: 1.279,
&quot;e10&quot;: 1.249,
&quot;isOpen&quot;: true,
&quot;houseNumber&quot;: &quot;&quot;,
&quot;postCode&quot;: 31319
},
{
&quot;id&quot;: &quot;5bf0bee3-4dc0-4f6e-a846-cb0ac3ac2efc&quot;,
&quot;name&quot;: &quot;Aral Tankstelle&quot;,
&quot;brand&quot;: &quot;ARAL&quot;,
&quot;street&quot;: &quot;Iltener Stra&#223;e&quot;,
&quot;place&quot;: &quot;Sehnde&quot;,
&quot;lat&quot;: 52.3173637,
&quot;lng&quot;: 9.959905,
&quot;dist&quot;: 3.2,
&quot;diesel&quot;: 1.139,
&quot;e5&quot;: 1.319,
&quot;e10&quot;: 1.289,
&quot;isOpen&quot;: true,
&quot;houseNumber&quot;: &quot;8&quot;,
&quot;postCode&quot;: 31319
},
{
&quot;id&quot;: &quot;096ac8b7-190d-4def-a860-6d2002f4b84e&quot;,
&quot;name&quot;: &quot;M1 Lehrte&quot;,
&quot;brand&quot;: &quot;M1&quot;,
&quot;street&quot;: &quot;Everner Str.&quot;,
&quot;place&quot;: &quot;Lehrte&quot;,
&quot;lat&quot;: 52.36773,
&quot;lng&quot;: 9.9967,
&quot;dist&quot;: 5.6,
&quot;diesel&quot;: 1.109,
&quot;e5&quot;: null,
&quot;e10&quot;: null,
&quot;isOpen&quot;: true,
&quot;houseNumber&quot;: &quot;41&quot;,
&quot;postCode&quot;: 31275
},
{
&quot;id&quot;: &quot;e1a15081-25ed-9107-e040-0b0a3dfe563c&quot;,
&quot;name&quot;: &quot;Sehnde, Kirchstr. 17&quot;,
&quot;brand&quot;: &quot;HEM&quot;,
&quot;street&quot;: &quot;Kirchstr.&quot;,
&quot;place&quot;: &quot;Sehnde&quot;,
&quot;lat&quot;: 52.34744,
&quot;lng&quot;: 9.928732,
&quot;dist&quot;: 6.2,
&quot;diesel&quot;: 1.109,
&quot;e5&quot;: 1.269,
&quot;e10&quot;: null,
&quot;isOpen&quot;: true,
&quot;houseNumber&quot;: &quot;17&quot;,
&quot;postCode&quot;: 31319
},
{
&quot;id&quot;: &quot;375a1dbb-4d61-4a4b-825a-3b2e3c0b82d8&quot;,
&quot;name&quot;: &quot;Lehrte , Ahltener Stra&#223;e 15&quot;,
&quot;brand&quot;: &quot;bft&quot;,
&quot;street&quot;: &quot;Ahltener Stra&#223;e 15&quot;,
&quot;place&quot;: &quot;Lehrte&quot;,
&quot;lat&quot;: 52.372474,
&quot;lng&quot;: 9.97253,
&quot;dist&quot;: 6.5,
&quot;diesel&quot;: 1.109,
&quot;e5&quot;: 1.309,
&quot;e10&quot;: 1.279,
&quot;isOpen&quot;: true,
&quot;houseNumber&quot;: &quot;&quot;,
&quot;postCode&quot;: 31275
},
{
&quot;id&quot;: &quot;45b00a42-93fa-41fe-9f9b-977b25a9832f&quot;,
&quot;name&quot;: &quot;OIL! Tankstelle Lehrte&quot;,
&quot;brand&quot;: &quot;OIL!&quot;,
&quot;street&quot;: &quot;Mielestr. 20&quot;,
&quot;place&quot;: &quot;Lehrte&quot;,
&quot;lat&quot;: 52.3804,
&quot;lng&quot;: 10.0061,
&quot;dist&quot;: 6.9,
&quot;diesel&quot;: 1.109,
&quot;e5&quot;: 1.279,
&quot;e10&quot;: 1.249,
&quot;isOpen&quot;: true,
&quot;houseNumber&quot;: &quot;&quot;,
&quot;postCode&quot;: 31275
}
]
}

MainActivity.java:

package com.example.benzinpreise;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.Result;
import retrofit2.*;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private TextView textViewResult;
private List&lt;Tankstellen&gt; tankstellenList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewResult =findViewById(R.id.text_view_result);
getTankstellen();
}
private void getTankstellen(){
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(TankstellenApi.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
TankstellenApi tankstellenApi = retrofit.create(TankstellenApi.class);
Call&lt;Tankstellen&gt; call = tankstellenApi.getTankstellen(&quot;57.001&quot;,&quot;10.007&quot;,&quot;7&quot;,&quot;all&quot;,&quot;dist&quot;,TankstellenApi.API_KEY);
call.enqueue(new Callback&lt;Tankstellen&gt;() {
@Override
public void onResponse(Call&lt;Tankstellen&gt; call, Response&lt;Tankstellen&gt; response) {
String displayResponse = &quot;&quot;;
String displayStations =&quot;&quot;;
Tankstellen antwort = response.body();
List&lt;Tankstellen.Stations&gt; tankstellenList = antwort.stationsList;
displayResponse += &quot;ok: &quot; +antwort.isOk() +&quot;\n&quot; + &quot;license: &quot;+antwort.getLicense() +&quot;\n&quot;+ &quot;data: &quot;+antwort.getData() +&quot;\n&quot;+ &quot;status: &quot;+antwort.getStatus() +&quot;\n&quot;;
for(Tankstellen.Stations stationen : tankstellenList){
displayResponse += stationen.id + &quot; &quot;
+ stationen.name + &quot; &quot;
+ stationen.brand + &quot; &quot;
+ stationen.street + &quot; &quot;
+ stationen.place + &quot; &quot;
+ stationen.lat + &quot; &quot;
+ stationen.lng + &quot; &quot;
+ stationen.dist + &quot; &quot;
+ stationen.diesel + &quot; &quot;
+ stationen.e5 + &quot; &quot;
+ stationen.e10 + &quot; &quot;
+ stationen.isOpen + &quot; &quot;
+ stationen.houseNumber + &quot; &quot;
+ stationen.postCode;
}
textViewResult.setText(displayResponse);
}
@Override
public void onFailure(Call&lt;Tankstellen&gt; call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
}

Tanktstellen.java

package com.example.benzinpreise;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class Tankstellen {
private boolean ok;
private String license;
private String data;
private String status;
@SerializedName(&quot;stations&quot;)
public List &lt;Stations&gt; stationsList = null;
public class Stations{
@SerializedName(&quot;id&quot;)
public String id;
public String name;
public String brand;
public String street;
public String place;
public double lat;
public double lng;
public double dist;
public double diesel;
public double e5;
public double e10;
public boolean isOpen;
public String houseNumber;
public int postCode;
}
public boolean isOk() {
return ok;
}
public String getLicense() {
return license;
}
public String getData() {
return data;
}
public String getStatus() {
return status;
}
}

TankstellenAPI.java:

package com.example.benzinpreise;
import com.google.gson.JsonObject;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface TankstellenApi {
String BASE_URL = &quot;https://creativecommons.tankerkoenig.de/&quot;;
String API_KEY = &quot;50d25698-e799-22df-c47c-833b7ae62b8b&quot;;
@GET(&quot;json/list.php&quot;)
Call&lt;Tankstellen&gt; getTankstellen (@Query(&quot;lat&quot;) String lat,
@Query(&quot;lng&quot;) String lng,
@Query(&quot;rad&quot;) String umkreis,
@Query(&quot;type&quot;) String type,
@Query(&quot;sort&quot;) String filter,
@Query(&quot;apikey&quot;) String apikey);
}

In the code are some german keywords just for me so i can understand the code a little bit better. I hope there are not confusing.

My Problem:
Im only getting

ok: true
license: CC BY 4.0 -  https:\/\/creativecommons.tankerkoenig.de
data: MTS-K
status: ok

The array is not iterating trough the for loop to display the array of petrol stations. I don't know why. Can anyone help me with this?

答案1

得分: 0

我克隆了你的代码并运行了。错误出现在你的回应中。

Antwort.stationslist.size = 0

这就是为什么它没有在循环中迭代,无法显示加油站数组。

英文:

I cloned your code and ran. The error is in your response.

Antwort.stationslist.size = 0

That's why it is not iterating trough the for loop to display the array of petrol stations.

huangapple
  • 本文由 发表于 2020年3月17日 01:38:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/60710680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定