• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Volley Web Service

AliYilmaz

Lurker
Hello friends. I am trying to pull data from json webservist with Volley and Genson. But it does not give an error, but it does not pull data. The problem is here.


Java:
private void GetData()
{
RequestQueue queue = Volley.newRequestQueue(this);
final String url = "https://api.github.com/users/mralexgray/repos";
StringRequest postRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
ParseData(response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", error.getLocalizedMessage());
}
}
)
{
};
queue.add(postRequest);
}
private void ParseData(String data)
{
Genson genson = new Genson();
try {
Servis[] persons = genson.deserialize(data, Servis[].class);
}
catch (Exception e)
{
String hata = e.getLocalizedMessage();
}
}
public class Servis implements Serializable {
public String id;
public String name;
public String full_name;
}
 
For example I want to get only below class.

  1. public class Servis implements Serializable {
  2. public String id;
  3. public String name;
  4. public String full_name
  5. }


nWXjqB.png
 
Run the app in debug mode, set a breakpoint in method ParseData() and step through the code. What happens?
 
it comes up here below..

StringRequest postRequest = new StringRequest(Request.Method.GET, url,

after go to..

queue.add(postRequest);

I dont get any error. Why do that...
 
Please indent you code properly. It's very hard to read.
 
I can do this much. Can you try your project.

Java:
private void GetData()
{
      RequestQueue queue =
      Volley.newRequestQueue(this);
      final String url =      
         "https://api.github.com/users/mralexgray/repos";
          StringRequest postRequest = new
        StringRequest(Request.Method.GET, url,
     new Response.Listener<String>()
  {
@Override
    public void onResponse(String response) {
   // response
     Log.d("Response", response);
    ParseData(response);
}
},
     new Response.ErrorListener()
{
     @Override
    public void onErrorResponse(VolleyError error) {
   // error
   Log.d("Error.Response",
   error.getLocalizedMessage());
}
}
)
{
};
    queue.add(postRequest);
}
     private void ParseData(String data)
    {
     Genson genson = new Genson();
    try {
     Servis[] persons = genson.deserialize(data,
     Servis[].class);
}
catch (Exception e)
{
     String hata = e.getLocalizedMessage();
}
}
   public class Servis implements Serializable {
   public String id;
   public String name;
   public String full_name;
}
 
Sorry your code is still quite incomprehensible as the brackets are all on the same level, meaning it's difficult to match things up.
Also you haven't clearly explained the problem. Can you please answer my previous question - have you set a breakpoint in the code and verified that it executes ParseData(), or what happens otherwise?
I appreciate that English is probably not your first language, but at the moment I just don't understand what the problem is.
 
This code. Can you try it.

it comes up here below..

StringRequest postRequest = new StringRequest(Request.Method.GET, url,

after go to..

queue.add(postRequest);

I dont get any error. Why do that...


Java:
  private static final String TAG = MainActivity.class.getName();
    Button btn;
    TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.button);
        txt = (TextView) findViewById(R.id.textView);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
                String url = "http://www.mocky.io/v2/59a87bcb1200009b028df65a";
                StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                // Display the first 500 characters of the response string.
                                txt.setText("Response is: "+ response.substring(0,500));
                                Log.i(TAG,"Response: "+ response.toString());

                            }
                        },

                        new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                        txt.setText("That didn't work!");
                            Log.i(TAG,"Error: "+ error.toString());

                    }
                });
                queue.add(stringRequest);
            }
        });

    }
 
Back
Top Bottom