Created
August 11, 2014 11:03
-
-
Save SZooo/cbc28c26ddbbeae38af8 to your computer and use it in GitHub Desktop.
Get data from the network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class bigTest extends Activity { | |
private Button btn; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.big); | |
btn = (Button) findViewById(R.id.btn); | |
btn.setOnClickListener(new btnListener()); | |
} | |
class btnListener implements OnClickListener { | |
@Override | |
public void onClick(View v) { | |
networker net = new networker(); | |
net.start(); | |
} | |
} | |
class networker extends Thread { | |
@Override | |
public void run() { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpGet httpGet = new HttpGet("http://www.marschen.com/data1.html"); | |
try { | |
HttpResponse hrs = httpClient.execute(httpGet); | |
int code = hrs.getStatusLine().getStatusCode(); | |
if (code == 200) { | |
HttpEntity entity = hrs.getEntity(); | |
InputStream is = entity.getContent(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
String line = br.readLine(); | |
Log.d("Http" ,"从服务器取得数据" + line); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment