Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package com.example.frogs.json;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
Button btnJson;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

// Tag used to cancel the request
String tag_json_arry = "json_array_req";

String url = "http://api.androidhive.info/volley/person_array.json";

final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();

JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<jsonarray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("", response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("", "Error: " + error.getMessage());
pDialog.hide();
}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(req, tag_json_arry);

String string ="{\"stats\" :true , \"Error\" :\"\",\"NO_OF_USER\" :50}";
btnJson=(Button)findViewById(R.id.btnJson);
btnJson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Tag used to cancel the request
String tag_json_obj = "json_obj_req";

String url = "http://api.androidhive.info/volley/person_object.json";

final ProgressDialog pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.show();

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<jsonobject>() {

@Override
public void onResponse(JSONObject response) {
Log.d("", response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("", "Error: " + error.getMessage());
// hide the progress dialog
pDialog.hide();
}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
});




}

}

What I have tried:

i try to connect the server but i can't be kindly asap send me the solution?
Posted
Comments
Richard MacCutchan 29-Feb-16 11:47am    
Soryy, but that is not the way to ask a question. You need to explain exactly what the problem is, and where it occurs.
Arindam Tewary 8-Mar-16 9:56am    
AppController is expected to be a application level class. Have you defined that ?
Arindam Tewary 8-Mar-16 9:57am    
Apart from that please check the JSON URL, directly from browser if that is indeed returning result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900