Httpclient
2015-09-14 21:19
393 查看
public class ConnectionActivity extends Activity implements View.OnClickListener{ private Button mButtonHttpClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_connection); mButtonHttpClient= (Button) findViewById(R.id.button_httpclient); mButtonHttpClient.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button_httpclient: MyAsynycTask task=new MyAsynycTask(); task.execute(); break; default: break; } } class MyAsynycTask extends AsyncTask<String,Integer,String>{ @Override protected String doInBackground(String... params) { HttpClient client=new DefaultHttpClient(); HttpGet get=new HttpGet("http://192.168.0.75:8080/WebDay12/Myservlet?username=zhangsan"); get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); try { get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); HttpResponse response=client.execute(get); int code=response.getStatusLine().getStatusCode(); if(code== HttpURLConnection.HTTP_OK){ System.out.println(code); HttpEntity entity=response.getEntity(); InputStream is=entity.getContent(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String line=br.readLine(); while(line!=null){ System.out.println(line); line=br.readLine(); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "连接成功"; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); mButtonHttpClient.setText(s); } } }
相关文章推荐
- n-1位数(http://acm.nyist.net/JudgeOnline/problem.php?pid=96)
- 网络连接之——xUtils 介绍(三)
- Android0912<十七>(Android 网络URLConnection)(ing多线程下载)
- Android网络连接---xUtils(HttpUtils)
- Android网络技术
- hdu 5438 Ponds(长春网络赛 拓扑+bfs)
- 第三十八天 网络连接:Volley、xUtils
- Java网络编程之Socket的使用
- HDU 5442 Favorite Donut (2015年长春赛区网络赛F题)
- Android网络连接---Google网络库Volley
- 超文本传输协议,http请求过程
- android HttpClient的使用
- HDU 5438 Ponds(2015ACM长春网络赛+枚举删点+DFS求联通块)
- Machine Learning 网络资源
- fcntl使用 http://www.cnblogs.com/andtt/articles/2178875.html
- TCP/IP协议相关知识
- Android网络连接---多线程下载
- android网络连接Volley
- Android学习记录之Volley网络通信框架基础解析(1)
- 2015.9.13 VIM、权限和网络管理