[英]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 :
这是我的代码:
this is json output on my local server : http://localhost:8080/KokosinjacRestfull/rest/textService/mudriPevci
这是我本地服务器上的 json 输出:
http://localhost:8080/KokosinjacRestfull/rest/textService/mudriPevci
[{"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"}]
[{"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", "日期":"2016-06-13"}]
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;
包kokosinjac.com.digiart.koktest.retrofit;
import java.util.ArrayList;
导入 java.util.ArrayList;
import kokosinjac.com.digiart.koktest.models.Text;
进口kokosinjac.com.digiart.koktest.models.Text; import retrofit2.Call;
进口改造2.Call; import retrofit2.http.GET;
进口改造2.http.GET; import retrofit2.http.Path;
导入retrofit2.http.Path;
public interface RetrofitAPInterface {
公共接口 RetrofitAPInterface {
@GET("rest/textService/{subCategory}") Call<ArrayList<Text>> getText(@Path("subCategory") String subCat);
}
}
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:在手机上显示数据的类(你不需要关注一些字符串,看看改造部分,我已经尽可能简单了,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.我知道整组数据都将转到一个简单的 label ,但它用于测试目的。 Still i can not retrieve anything and i do not get any errors as well.
我仍然无法检索任何东西,我也没有收到任何错误。 Help would be much appreciated.
帮助将不胜感激。 Thanks!
谢谢!
I think problem with your URL if you are testing your App with android emulator then try like "http://10.0.2.2:8080/"
.如果您使用 android 模拟器测试您的应用程序,我认为您的 URL有问题,然后尝试使用
"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/"
.但是如果您正在使用设备进行测试,那么您需要传递您的机器 IP 地址,例如
"http://192.143.1.0/"
。 and make sure that your device is connected with your machine on which your database is exits.并确保您的设备已连接到您的数据库所在的计算机。
问题可能出在您的 BASE_URL 将您的 BASE_URL 更改为BASE_URL ="http://<your_local_ip_address>/KokosinjacRestfull/";
i hope the below example might help我希望下面的例子可能会有所帮助
public void startFetching() {
mApiManager.getFlowerApi().getFlowers(new Callback<String>() {
@Override
public void success(String s, Response response) {
Log.d(TAG, "JSON :: " + s);
try {
JSONArray array = new JSONArray(s);
for(int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
Flower flower = new Flower.Builder()
.setCategory(object.getString("category"))
.setPrice(object.getDouble("price"))
.setInstructions(object.getString("instructions"))
.setPhoto(object.getString("photo"))
.setName(object.getString("name"))
.setProductId(object.getInt("productId"))
.build();
mListener.onFetchProgress(flower);
}
} catch (JSONException e) {
mListener.onFetchFailed();
}
mListener.onFetchComplete();
}
@Override
public void failure(RetrofitError error) {
Log.d(TAG, "Error :: " + error.getMessage());
mListener.onFetchComplete();
}
});
}
the problem is with your URL Android can't hit your localhost ie http://localhost:8080 /KokosinjacRestfull/rest/textService/mudriPevci问题在于您的 URL Android 无法访问您的本地主机,即http://localhost:8080 /KokosinjacRestfull/rest/textService/mudriPevci
up your server in specific IP and the run.The new IP would be like http://192.168.1.1 /KokosinjacRestfull/rest/textService/mudriPevci在特定 IP 中启动您的服务器并运行。新 IP 将类似于http://192.168.1.1 /KokosinjacRestfull/rest/textService/mudriPevci
使用 ip 地址而不是 localhost(从 url 替换 localhost 并将您的计算机的 ipaddress 作为 ip 地址打开 cmd 并键入 ipconfig 并替换 localhost)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.