I am trying to implement Fetch list of data using Retrofit, It’s not working, It shows blank screen, I can’t able to get data, My Json Response is as below
[
{
"code": 1,
"message": "listed!",
"totalrecord": 2,
"result": [
{
"FieldTypeID": "2",
"Name": "Marketing"
},
{
"FieldTypeID": "60",
"Name": "Designing"
},
{
"FieldTypeID": "61",
"Name": "SEO"
},
{
"FieldTypeID": "78",
"Name": "Mobile App Development"
}
]
}
]
MainActivity.java
public class MainActivity extends ListActivity {
ArrayList<Result> flowerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint("http://services.hanselandpetal.com").build();
final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint(BASE_URL).build();
api flowerapi = restadapter.create(api.class);
flowerapi.getData(new Callback<ArrayList<Result>>() {
@Override
public void success(ArrayList<Result> flowers, Response response) {
flowerList = flowers;
Log.d("ArrayList:: :: :::", String.valueOf(flowerList.size()));
adapter adapt = new adapter(getApplicationContext(), R.layout.item_file, flowerList);
//ListView listView = (ListView) findViewById(R.id.list);
setListAdapter(adapt);
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
}
});
}
Interface is api.java
public interface api {
// @GET("/feeds/flowers.json")
@GET("/GetUserGroupList")
public void getData(Callback<ArrayList<Result>> response);
}
Result.Java
public class Result {
@SerializedName("FieldTypeID")
@Expose
private String FieldTypeID;
@SerializedName("Name")
@Expose
private String Name;
/**
*
* @return
* The FieldTypeID
*/
public String getFieldTypeID() {
return FieldTypeID;
}
/**
*
* @param FieldTypeID
* The FieldTypeID
*/
public void setFieldTypeID(String FieldTypeID) {
this.FieldTypeID = FieldTypeID;
}
/**
*
* @return
* The Name
*/
public String getName() {
return Name;
}
/**
*
* @param Name
* The Name
*/
public void setName(String Name) {
this.Name = Name;
}
}
Flower.java
public class Flower {
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("totalrecord")
@Expose
private Integer totalrecord;
@SerializedName("result")
@Expose
private List<Result> result = new ArrayList<Result>();
/**
*
* @return
* The code
*/
public Integer getCode() {
return code;
}
/**
*
* @param code
* The code
*/
public void setCode(Integer code) {
this.code = code;
}
/**
*
* @return
* The message
*/
public String getMessage() {
return message;
}
/**
*
* @param message
* The message
*/
public void setMessage(String message) {
this.message = message;
}
/**
*
* @return
* The totalrecord
*/
public Integer getTotalrecord() {
return totalrecord;
}
/**
*
* @param totalrecord
* The totalrecord
*/
public void setTotalrecord(Integer totalrecord) {
this.totalrecord = totalrecord;
}
/**
*
* @return
* The result
*/
public List<Result> getResult() {
return result;
}
/**
*
* @param result
* The result
*/
public void setResult(List<Result> result) {
this.result = result;
}
}
Please help me !!!
Log:
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ <--- HTTP 200 http://203.124.107.14:39/VrinsoftCRM.asmx/GetUserGroupList (242ms)
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Cache-Control: private, max-age=0
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Content-Length: 699
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Server: Microsoft-IIS/7.5
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ X-AspNet-Version: 2.0.50727
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ X-Powered-By: ASP.NET
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Date: Thu, 14 Apr 2016 09:48:54 GMT
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ OkHttp-Selected-Protocol: http/1.1
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ OkHttp-Sent-Millis: 1460627392316
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ OkHttp-Received-Millis: 1460627392436
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ [{"code":1,"message":"listed!","totalrecord":2,"result":[{"FieldTypeID": "2","Name": "Marketing" },{"FieldTypeID": "60","Name": "Designing" },{"FieldTypeID": "61","Name": "SEO" },{"FieldTypeID": "78","Name": "Mobile App Development" },{"FieldTypeID": "79","Name": ".Net Development" },{"FieldTypeID": "81","Name": "QA" },{"FieldTypeID": "94","Name": "HR" },{"FieldTypeID": "100","Name": "Business Analyst" },{"FieldTypeID": "101","Name": "Business Development Executive" },{"FieldTypeID": "131","Name": "Network Engg" },{"FieldTypeID": "142","Name": "Facebook" },{"FieldTypeID": "156","Name": "PHP Development" },{"FieldTypeID": "157","Name": "Google" },{"FieldTypeID": "158","Name": "Google 2" }]}]
04-14 05:49:52.434 1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ <--- END HTTP (699-byte body)
04-14 05:49:52.434 1375-1375/com.example.android.listviewretrofit D/Suceess :: ::﹕ [ 04-14 05:49:52.434 1375: 1375 D/mListResult size :: :: ]
1
2
Answers
You need create one helper class
Then change api method:
Try this code below
Create class ApiService.class
Create CleanGsonConverter.class
Create CustomErrorHandler.class
Create JsonTypedInput.class
add to gradle.build
in MainActivity.class
class interface
You should create customAdapter
in MainActivity
in activity_main.xml. You must change id of list view
and in method onPostExecute