简体   繁体   English

与Volley联网

[英]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. 我有2类Network.class和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. 我在MainActivity类中有一个TextView,我希望将其替换为从网络类的网络调用中获得的文本。 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? 我当前遇到的问题是,在应用程序启动时加载MainActivity类时,无法在网络类中发起网络调用吗?

Below is the Code to MainActivity: 以下是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. 下面是我要进行网络调用的网络类,响应将需要替换MainActivity类中TextView中的文本。

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? 我是Android的新手,正在努力在MainActivity的第二个活动中调用postData方法? The issue I get is that the TextView has text hard coded in the XML but when I run the Application it is blank? 我得到的问题是TextView在XML中使用硬编码的文本,但是当我运行应用程序时,它是空白的? 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? 就像是,响应还是空白,但我对此表示怀疑,因为我在网络类(System.out.println(“ +++++++++++++++++++++ +++++++++++++++++++++++++++ userName =“ + userName);)没有显示在终端中,这使我认为它没有运行postData方法根本不起作用,或者响应不起作用,但是它只是将TextView设置为空白?

You cannot change the GUI from an async-task. 您不能从异步任务更改GUI。 As JsonObjectRequest works asynchronous you should run tv.setText(jsonResponse); 由于JsonObjectRequest异步工作,因此您应该运行tv.setText(jsonResponse); on the main thread using: 在主线程上使用:

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

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. 根据我的评论,您在终端中看不到任何内容的原因是因为您没有调用postData方法,所以它从未执行。

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. 使Network成为普通班而不是Activity。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM