第一步:在MainActivity.xml中添加 interNet 权限
<uses-permission android:name="android.permission.INTERNET" /> 第二步:在gradle中导入依赖包
compile ‘com.mcxiaoke.volley:library:1.0.19‘ 第三步:创建Java和XML文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 7 <Button 8 android:id="@+id/start" 9 android:layout_width="match_parent" 10 android:layout_height="50dp" 11 android:text="start" 12 /> 13 14 <Button 15 android:id="@+id/end" 16 android:layout_width="match_parent" 17 android:layout_height="50dp" 18 android:text="end" 19 android:layout_below="@+id/start" 20 /> 21 <Button 22 android:layout_width="match_parent" 23 android:layout_height="50dp" 24 android:id="@+id/clear" 25 android:layout_below="@+id/end" 26 android:text="clear" 27 /> 28 <Button 29 android:layout_width="match_parent" 30 android:layout_height="50dp" 31 android:id="@+id/jump" 32 android:layout_below="@+id/clear" 33 android:text="jump" 34 /> 35 <TextView 36 android:id="@+id/text_view" 37 android:layout_width="match_parent" 38 android:layout_height="match_parent" 39 android:layout_below="@+id/jump" 40 /> 41 </RelativeLayout>
1 package homepage.yhm.com.voellydemo; 2 3 import android.app.Activity; 4 import android.app.DownloadManager; 5 import android.content.Intent; 6 import android.os.Handler; 7 import android.os.Bundle; 8 import android.os.Message; 9 import android.util.Log; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.TextView; 13 14 import com.android.volley.Request; 15 import com.android.volley.Response; 16 import com.android.volley.VolleyError; 17 import com.android.volley.toolbox.StringRequest; 18 import com.android.volley.toolbox.Volley; 19 20 21 public class MainActivity extends Activity { 22 23 private TextView text_view = null; 24 private Button start = null; 25 private Button end = null; 26 private Button clear = null; 27 private Button jump = null; 28 29 //使用handler时首先要创建一个handler 30 Handler handler = new Handler(){ 31 @Override 32 public void handleMessage(Message msg) { 33 text_view.setText(msg.obj.toString()); 34 // switch (msg.what){ 35 // case 1: 36 // text_view.setText(msg.obj.toString()); 37 // break; 38 // case 2: 39 // text_view.setText(msg.obj.toString()); 40 // break; 41 // } 42 } 43 }; 44 //要用handler来处理多线程可以使用runnable接口,这里先定义该接口 45 //线程中运行该接口的run函数 46 Runnable update_thread = new Runnable() { 47 public void run() { 48 //线程每次执行时输出"UpdateThread..."文字,且自动换行 49 //textview的append功能和Qt中的append类似,不会覆盖前面 50 //的内容,只是Qt中的append默认是自动换行模式 51 // text_view.append("\nUpdateThread..."); 52 //延时1s后又将线程加入到线程队列中 53 // handler.postDelayed(update_thread, 1000); 54 // 55 56 // StringRequest stringRequest = new StringRequest(Request.Method.GET,"http://www.baidu.com", new Response.Listener<String> (){ 57 // public void onResponse(String response){ 58 // Message message = handler.obtainMessage(); 59 // message.obj = response; 60 // message.what = 1; 61 // handler.sendMessage(message); 62 // Log.d("baidu",response); 63 // }; 64 // }, new Response.ErrorListener() { 65 // @Override 66 // public void onErrorResponse(VolleyError error) { 67 // Message message = handler.obtainMessage(); 68 // message.obj = error.getMessage(); 69 // message.what = 2; 70 // handler.sendMessage(message); 71 // } 72 // }); 73 Volley.newRequestQueue(MainActivity.this).add(new StringRequest(Request.Method.GET,"http://www.baidu.com", new Response.Listener<String> (){ 74 public void onResponse(String response){ 75 Message message = handler.obtainMessage(); 76 message.obj = response; 77 message.what = 1; 78 handler.sendMessage(message); 79 Log.d("baidu",response); 80 }; 81 }, new Response.ErrorListener() { 82 @Override 83 public void onErrorResponse(VolleyError error) { 84 Message message = handler.obtainMessage(); 85 message.obj = error.getMessage(); 86 message.what = 2; 87 handler.sendMessage(message); 88 } 89 })); 90 91 92 93 } 94 }; 95 96 @Override 97 public void onCreate(Bundle savedInstanceState) { 98 super.onCreate(savedInstanceState); 99 setContentView(R.layout.activity_main); 100 101 text_view = (TextView) findViewById(R.id.text_view); 102 start = (Button) findViewById(R.id.start); 103 start.setOnClickListener(new StartClickListener()); 104 end = (Button) findViewById(R.id.end); 105 end.setOnClickListener(new EndClickListener()); 106 107 clear = (Button) findViewById(R.id.clear); 108 clear.setOnClickListener(new View.OnClickListener() { 109 @Override 110 public void onClick(View v) { 111 text_view.setText(null); 112 } 113 }); 114 115 // 116 jump = (Button) findViewById(R.id.jump); 117 jump.setOnClickListener(new View.OnClickListener() { 118 @Override 119 public void onClick(View v) { 120 Intent i = new Intent(); 121 i.setClass(MainActivity.this, Login.class); 122 startActivity(i); 123 } 124 }); 125 126 } 127 128 private class StartClickListener implements View.OnClickListener { 129 public void onClick(View v) { 130 // TODO Auto-generated method stub 131 //将线程接口立刻送到线程队列中 132 handler.post(update_thread); 133 } 134 } 135 136 private class EndClickListener implements View.OnClickListener { 137 public void onClick(View v) { 138 // TODO Auto-generated method stub 139 //将接口从线程队列中移除 140 handler.removeCallbacks(update_thread); 141 } 142 143 } 144 145 }
时间: 2024-10-10 18:49:52