Retrofit show json Response in ListView very easy and short
I love this, understand life as it is.
Download app:
Show json Response in ListView (Retrofit):
Show json Response in ListView (Retrofit):
Step 1 : create Rest Interface like below attached find it.
Step 1 : create Rest Interface like below attached find it.
Step 2 : create MainActivity like below attached find it.
Step 2 : create MainActivity like below attached find it.
Step 3 : create layout file like below attached find it.
Step 3 : create layout file like below attached find it.
Step 4: create gradle add it and (Sync) it.
Step 4: create gradle add it and (Sync) it.
Step 4 : Run execute and watch the output in layout. Over.
Step 4 : Run execute and watch the output in layout. Over.
Step 1:
Step 1:
public interface DetailsAPI {
@GET("/key/value/one/two")
void insertUser(Callback<JsonObject> jsonObjectCallback);
}
Step 2:
Step 2:
public class MainActivity extends AppCompatActivity {
TextView tv;
ListView listView;
List<String> stringList = new ArrayList<>();
public static final String Root_url = "http://echo.jsontest.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv_response);
listView = (ListView) findViewById(R.id.listview);
insertUser();
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, stringList);
listView.setAdapter(stringArrayAdapter);
}
private void insertUser() {
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(Root_url).build();
DetailsAPI api = adapter.create(DetailsAPI.class);
api.insertUser(new Callback<JsonObject>() {
@Override
public void success(JsonObject jsonObject, Response response) {
if (response.getStatus() == 200)
tv.setText("You got the data");
String jsonResponse = jsonObject.get("one").getAsString();
stringList.add(jsonResponse);
jsonResponse = jsonObject.get("key").getAsString();
stringList.add(jsonResponse);
//*********************Json response is too short so the same above values are stored in list array repeatedly.... **************
jsonResponse = jsonObject.get("one").getAsString();
stringList.add(jsonResponse);
jsonResponse = jsonObject.get("key").getAsString();
stringList.add(jsonResponse);
jsonResponse = jsonObject.get("one").getAsString();
stringList.add(jsonResponse);
jsonResponse = jsonObject.get("key").getAsString();
stringList.add(jsonResponse);
}
public void failure(RetrofitError error) {
Toast.makeText(MainActivity.this, "try again!", Toast.LENGTH_SHORT).show();
}
});
}
}
Step 3:
Step 3:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Response here"
android:textColor="#ff2323"
android:textSize="22sp"
android:id="@+id/tv_response" />
<ListView
android:id="@+id/listview"
android:layout_below="@+id/tv_response"
android:layout_width="match_parent"
android:layout_height="100dp">
</ListView>
</RelativeLayout>
Step 4:
Step 4:
after put this line don't forget to sync it.
dependencies {
compile 'com.squareup.retrofit:retrofit:1.9.0'
}