如何在Android上通过翻新来解析JSON数据 [英] How to parse json data with retrofit on Android
问题描述
请尝试在此处具体说明.因此,我已经创建了最基本的代码来进行测试,但仍然无法获取数据,请帮忙!这是我的代码:
ill try to be specific here. So i have created the most basic possible code just to test it and i am still unable to get the data, please help! Here is my code :
这是我的本地服务器上的json输出:
http://localhost:8080/KokosinjacRestfull/rest/textService/mudriPevci
this is json output on my local server :
http://localhost:8080/KokosinjacRestfull/rest/textService/mudriPevci
[{"id":1,"subCategory":"MudriPevci","title":"Mujo i haso",说明":"Krenuli do Grada," author:" luka," date:" 2016-06-13},{" id:3," subCategory:" mudriPevci," title:" Perica," description":"Pa on je napravio Haos," author:" luka," date:" 2016-06-13}]
[{"id":1,"subCategory":"MudriPevci","title":"Mujo i haso","description":"Krenuli do Grada","author":"luka","date":"2016-06-13"},{"id":3,"subCategory":"mudriPevci","title":"Perica","description":"Pa on je napravio Haos","author":"luka","date":"2016-06-13"}]
Text.class:
Text.class :
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:
Api interface.class :
package kokosinjac.com.digiart.koktest.retrofit;
package kokosinjac.com.digiart.koktest.retrofit;
import java.util.ArrayList;
import java.util.ArrayList;
导入kokosinjac.com.digiart.koktest.models.Text;进口 retrofit2.Call;导入retrofit2.http.GET;导入retrofit2.http.Path;
import kokosinjac.com.digiart.koktest.models.Text; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path;
公共接口RetrofitAPInterface {
public interface RetrofitAPInterface {
@GET("rest/textService/{subCategory}")
Call<ArrayList<Text>> getText(@Path("subCategory") String subCat);
}
在电话上显示数据的
类(您无需关注某些Strings,只需看一下翻新部分即可;我已经尽可能地简化了它, subCatData.class:
class that displays data on the phone (you do not need to pay attention on some Strings, just look at the retrofit part,i;ve made it as simple as i can, 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) {
}
});
}
}
我知道整个数据都将放在一个简单的标签上,但这只是出于测试目的.我仍然无法检索任何东西,也没有得到任何错误.帮助将不胜感激.谢谢!
I am aware that the whole bunch of data is going to a simple label , but it is for testing purposes. Still i can not retrieve anything and i do not get any errors as well. Help would be much appreciated. Thanks!
推荐答案
我认为您的URL问题,如果您使用android模拟器测试应用,然后尝试使用"http://10.0.2.2:8080/"
.,但是如果您正在使用设备进行测试,则需要传递您的计算机IP地址,例如"http://192.143.1.0/"
.,并确保您的设备已连接到退出数据库的计算机.
I think problem with your URL if you are testing your App with android emulator then try like "http://10.0.2.2:8080/"
. but if you are testing with device then you need to pass Your machine IP address like "http://192.143.1.0/"
. and make sure that your device is connected with your machine on which your database is exits.
这篇关于如何在Android上通过翻新来解析JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!