android多线程,在方法中执行

主类:

package com.example.thread_1;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {
	private Handler handler;
	private Button button1;
	private String result1 = null, result2 = null;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) findViewById(R.id.button1);
		final RunClass runClass = new RunClass();
		button1.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				Run1();// //桐城市
				Run2();// /舒城


				runClass.RunThread3();// /巢湖
				runClass.RunThread4();// /庐江
			}
		});


		handler = new Handler() {


			@Override
			public void handleMessage(Message msg) {
				// TODO 自动生成的方法存根
				// super.handleMessage(msg);
				if (result1 != null) {
					Log.i("result1", result1);
				}


			}


		};


	}// /onCreate


	public void Run1() {
		new Thread(new Runnable() {


			@Override
			public void run() {
				// TODO 自动生成的方法存根
				send1();
				Message m = new Message();
				handler.sendMessage(m);


			}


		}).start();


	}
///
	private void send1() {
		// TODO 自动生成的方法存根 ////桐城市
		String target1 = "http://api.map.baidu.com/geocoder?output=json&location=31,117&key=GlFh.......";
		URL url1;


		try {
			url1 = new URL(target1);


			HttpURLConnection conn1 = (HttpURLConnection) url1.openConnection();


			BufferedReader buffer1 = new BufferedReader(new InputStreamReader(
					conn1.getInputStream()));
			String inputLine1 = null;
			while ((inputLine1 = buffer1.readLine()) != null) {
				result1 += inputLine1 + "\n";
			}
		} catch (MalformedURLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}


	}// /send


	public void Run2() {
		new Thread(new Runnable() {


			@Override
			public void run() {
				// TODO 自动生成的方法存根 ////舒城
				String target2 = "http://api.map.baidu.com/geocoder?output=json&location=31.3,117&key=GlFh......";
				URL url2;


				try {
					url2 = new URL(target2);


					HttpURLConnection conn2 = (HttpURLConnection) url2
							.openConnection();


					BufferedReader buffer2 = new BufferedReader(
							new InputStreamReader(conn2.getInputStream()));
					String inputLine2 = null;
					while ((inputLine2 = buffer2.readLine()) != null) {
						result2 += inputLine2 + "\n";
					}
					Log.i("result2", result2);
					//
					// in_2.close();
					// conn_2.disconnect();


				} catch (MalformedURLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}


		}).start();


	}


}

另一个类:

<pre name="code" class="java">package com.example.thread_1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Message;
import android.util.Log;

public class RunClass extends Activity {
	private String result3 = null, result4 = null;
	
	public void RunThread3() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO 自动生成的方法存根
				send3();
				//Message m = new Message();
				//handler.sendMessage(m);

			}

		}).start();

	}
	///
	private void send3() {
		// TODO 自动生成的方法存根////巢湖市
		String target3 = "http://api.map.baidu.com/geocoder?output=json&location=31.9,117.8&key=Gl.....";////需要自己申请
		URL url3;

		try {
			url3 = new URL(target3);

			HttpURLConnection conn3= (HttpURLConnection) url3
					.openConnection();

			BufferedReader buffer3 = new BufferedReader(new InputStreamReader(conn3
					.getInputStream()));
			String inputLine3 = null;
			while ((inputLine3 = buffer3.readLine()) != null) {
				result3 += inputLine3 + "\n";
			}
		 Log.i("result3", result3);
			//
			// in_2.close();
			// conn_2.disconnect();

		} catch (MalformedURLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}// /send_2
	
	
	public void RunThread4() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO 自动生成的方法存根
				//send_3();
				//Message m = new Message();
				//handler.sendMessage(m);
				// TODO 自动生成的方法存根    ////庐江县
				String target4 = "http://api.map.baidu.com/geocoder?output=json&location=31.45,117.24&key=GlF.....";
				URL url4;

				try {
					url4 = new URL(target4);

					HttpURLConnection conn4 = (HttpURLConnection) url4
							.openConnection();
					BufferedReader buffer4 = new BufferedReader(new InputStreamReader(conn4
							.getInputStream()));
					String inputLine4 = null;
					while ((inputLine4 = buffer4.readLine()) != null) {
						result4 += inputLine4 + "\n";
					}
				 Log.i("result4", result4);
					//
					// in_2.close();
					// conn_2.disconnect();

				} catch (MalformedURLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
				
			}

		}).start();

	}
	///
	
	
	
	
}

 
 
加上网权限:   

 <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="16" />
   <uses-permission android:name="android.permission.INTERNET"/>

结果:结果有四个,这里只写出一个了,


你可能感兴趣的:(android,线程)