Here is solution, For this you have to use Volley Library to get data from Server.
Add this line in build.gradle(Module:app)
dependencies {....
compile 'com.mcxiaoke.volley:library:1.0.19'
}
Use this code in Main Activity
public void getJsonData() {
String tag = "get_json";
StringRequest request = new StringRequest(Request.Method.POST,
"YOUR URL TO GET JSON DATA", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Get response : " + response);
try {
JSONObject object = new JSONObject(response);
String name = object.getString("name");
String description = object.getString("description");
String url = object.getString("url");
JSONArray array = object.getJSONArray("interfaces");
for (int i = 0;i<array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
String name_2 = jsonObject.getString("name");
JSONArray methodArray = jsonObject.getJSONArray("methods");
for (int k=0;k<methodArray.length();k++){
JSONObject object_1 = methodArray.getJSONObject(k);
String name_3= object_1.getString("name");
String returnvalue = object_1.getString("returnvalue");
JSONArray parametersArray = object_1.getJSONArray("parameters");
for (int j=0;j<parametersArray.length();j++){
JSONObject jsonObject_1 = parametersArray.getJSONObject(j);
String name_4 = jsonObject_1.getString("name");
String type = jsonObject_1.getString("type");
}
}
}
Customelistadapter adapter = new Customelistadapter(MainActivity.this,data1,data3,data2);
lv.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
request.setRetryPolicy(new RetryPolicy() {
@Override
public void retry(VolleyError error) throws VolleyError {
}
@Override
public int getCurrentTimeout() {
return 0;
}
@Override
public int getCurrentRetryCount() {
return 0;
}
});
request.setShouldCache(false);
AppController.getInstance().addToRequestQueue(request, tag);
}
To run this method you have to add this line in onCreat of MainActivity
getJsonData();
Create Application Class
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
}
Now Edit in mainfest.xml inside Application tag add this
<application
android:name=".AppController">
Happy to help…
23
solved RestAPI JSON Parsing