zu analysieren, versuchen Sie, hier spezifisch zu sein. Also ich habe den grundlegendsten möglichen Code erstellt, nur um es zu testen, und ich bin immer noch nicht in der Lage, die Daten zu bekommen, bitte helfen Sie! Hier ist mein Code:Wie JSON-Daten mit Retrofit auf Android
dies json Ausgabe auf meinem lokalen Server: http://localhost:8080/KokosinjacRestfull/rest/textService/mudriPevci
[{ "id": 1, "Unter": "MudriPevci", "title": "Mujo i haso "," description ":" Krenuli do Grada "," Autor ":" luka "," Datum ":" 2016-06-13 "}, {" id ": 3," subCategory ":" mudriPevci ", "title": "Perica", "Beschreibung": "Pa auf je napravio Haos", "Autor": "luka", "Datum": "2016.06.13"}]
Text. Klasse:
package kokosinjac.com.digiart.koktest.models;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Text {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("subCategory")
@Expose
private String subCategory;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("author")
@Expose
private String author;
@SerializedName("date")
@Expose
private String date;
/**
* No args constructor for use in serialization
*
*/
/**
*
* @param id
* @param author
* @param title
* @param subCategory
* @param description
* @param date
*/
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The subCategory
*/
public String getSubCategory() {
return subCategory;
}
/**
*
* @param subCategory
* The subCategory
*/
public void setSubCategory(String subCategory) {
this.subCategory = subCategory;
}
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return
* The description
*/
public String getDescription() {
return description;
}
/**
*
* @param description
* The description
*/
public void setDescription(String description) {
this.description = description;
}
/**
*
* @return
* The author
*/
public String getAuthor() {
return author;
}
/**
*
* @param author
* The author
*/
public void setAuthor(String author) {
this.author = author;
}
/**
*
* @return
* The date
*/
public String getDate() {
return date;
}
/**
*
* @param date
* The date
*/
public void setDate(String date) {
this.date = date;
}
}
Api interface.class:
Paket kokosinjac.com.digiart.koktest.retrofit;
importieren java.util.ArrayList;
importieren kokosinjac.com.digiart.koktest.models.Text; Import Nachrüstung2.Anrufen; importiere retrofit2.http.GET; importiere retrofit2.http.Path;
öffentliche Schnittstelle RetrofitAPInterface {
@GET("rest/textService/{subCategory}") Call<ArrayList<Text>> getText(@Path("subCategory") String subCat);
}
Klasse, die Daten auf dem Handy anzeigt (Sie brauchen keine Aufmerksamkeit auf einige Strings zu zahlen, schauen Sie in die Retrofit-Teil, i; habe es so einfach gemacht, wie ich kann, subCatData.class:
public static final String BASE_URL ="http://localhost:8080/KokosinjacRestfull/";
HashMap<String,String> dataArr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_cat_data);
final TextView tView = (TextView) findViewById(R.id.textView);
Intent intent = getIntent();
String urlSecondPartBefore = intent.getStringExtra("passedSubCat");
String urlSecondPartAfter = urlSecondPartBefore.replaceAll("\\s", "");
String urlFirstPart = intent.getStringExtra("passedUrlFirstPart");
String catName = intent.getStringExtra("passedCatName");
String data = null;
// TextView test = (TextView) findViewById(R.id.test);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitAPInterface apiService = retrofit.create(RetrofitAPInterface.class);
Call<ArrayList<Text>> call = apiService.getText("mudriPevci");
call.enqueue(new Callback<ArrayList<Text>>() {
@Override
public void onResponse(Call<ArrayList<Text>> call, Response<ArrayList<Text>> response) {
int statusCode = response.code();
ArrayList<Text> textArray = response.body();
for (Text t : textArray){
tView.setText(t.getDescription());
}
// Log.i("DATA", "onResponse: "+text.getId());
}
@Override
public void onFailure(Call<ArrayList<Text>> call, Throwable t) {
}
});
}
}
Ich bin mir bewusst, dass die ganze Datenmenge auf ein einfaches Etikett geht, aber es ist für Testzwecke. Trotzdem kann ich nichts abrufen und bekomme auch keine Fehler. Hilfe wäre sehr willkommen. Vielen Dank!
diese Lösung kann Ihnen helfen, bitte überprüfen Sie es aus http://stackoverflow.com/questions/33338923/how-to-parse-retrofit-json-response?rq=1 –