Stillie
Stillie

Reputation: 2676

Networking with Volley

Please could you help me with a network request I am trying here.

I have 2 Classes Network.class and MainActivity.class. I have a TextView in the MainActivity Class that I would like to be replaced with the text I get from the Network call in the Network Class. Problem I am currently having is I cant initiate the network call in the Network Class when the MainActivity Class is loaded when the application starts?

Below is the Code to MainActivity:

public class MainActivity extends ActionBarActivity {



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

    TextView tv = (TextView)findViewById(R.id.text);
    String test = Network.userName;
    tv.setText(test);

  }


}

and below is the network class that I would like to do the network call and the response will need to replace the text in the TextView in the MainActivity Class.

Network Class:

public class Network extends Activity{

public static String userName;
private String jsonResponse;
String url_home = "http://www.someurl.com";


private void postData(final TextView tv) {

    final RequestQueue request = Volley.newRequestQueue(this);

    JsonObjectRequest postReq = new JsonObjectRequest(Request.Method.GET, url_home, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                jsonResponse = "";
                for(int i = 0; i< response.length(); i++) {
                    String userName = response.getString("DOTWBannerHD");

                    System.out.println("++++++++++++++++++++++++++++++++++++++++++++userName = " + userName);
                    jsonResponse += userName;

                    System.out.println("++++++++++++++++++++++++++++++++++++++++++++JsonResponse = " + jsonResponse);

                }

                tv.setText(jsonResponse);
            } catch (JSONException e){
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("Error [" + error + "]");

        }
    }) {
        @Override
        public Map getHeaders() throws AuthFailureError {
            Map headers = new HashMap();
            headers.put("Accept", "application/json");
            System.out.println(headers);
            return headers;
        }
    };
    request.add(postReq);

}


}

I am very new to Android and am battling to call the postData method from the second activity, in the MainActivity? The issue I get is that the TextView has text hard coded in the XML but when I run the Application it is blank? It's like, either the response is blank, but I doubt its that because the code I put in the Network Class (System.out.println("++++++++++++++++++++++++++++++++++++++++++++userName = " + userName);) isn't showing up in the Terminal which makes me think that its not running the postData method at all or the response is not working but it just sets the TextView to blank?

Upvotes: 0

Views: 75

Answers (2)

freddieptf
freddieptf

Reputation: 2234

Following up on my comment, the reason your are not seeing anything in the terminal is because you're not calling you postData method so it's never executed.

public class MainActivity extends ActionBarActivity {

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

    TextView tv = (TextView)findViewById(R.id.text);
    Network network = new Network();
    network.postData(tv);

  }

}

and make Network a normal class not an Activity.

public class Network{
 ////The variables and your postData method here
}   

Upvotes: 0

MD1948
MD1948

Reputation: 292

You cannot change the GUI from an async-task. As JsonObjectRequest works asynchronous you should run tv.setText(jsonResponse); on the main thread using:

runOnUiThread(new Runnable() {
    public void run() {
        tv.setText(jsonResponse);
    }
});

Upvotes: 1

Related Questions