一、使用HttpGet方式进行请求


private Button myButton = null;private HttpResponse httpResponse = null;private HttpEntity httpEntity = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myButton = (Button) findViewById(R.id.myButton);myButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stub//生成一个请求对象//参数:请求的地址HttpGet httpGet = new HttpGet("http://172.16.3.102:8080/Ibeacon/NewFile.jsp");//生成一个http客户端对象HttpClient httpClient = new DefaultHttpClient();InputStream inputStream = null;try {try {//获取请求的响应httpResponse = httpClient.execute(httpGet);//从响应当中获取数据流httpEntity = httpResponse.getEntity();inputStream = httpEntity.getContent();BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputStream));//保存获取到的数据String result = "";String line = "";while ((line = bufferedreader.readLine()) != null) {result += line;}System.out.println(result);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}});}

二、使用HttpPost方式进行请求

private Button myButton = null;//声明请求的链接private String myURL = "http://172.16.3.102:8080/Ibeacon/Receive";HttpResponse httpResponse = null;HttpEntity httpEntity = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myButton = (Button) findViewById(R.id.myButton);myButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stub//键值对//用来设置请求的参数NameValuePair nameValuePair1 = new BasicNameValuePair("uuid", "300001");NameValuePair nameValuePair2 = new BasicNameValuePair("classify", "200001");List<NameValuePair> lists = new ArrayList<NameValuePair>();lists.add(nameValuePair1);lists.add(nameValuePair2);try {//HttpEntity既可以是请求体也可以是响应体//设置请求的参数HttpEntity requesthttpEntity = new UrlEncodedFormEntity(lists);//生成一个请求Post请求对象HttpPost httppost = new HttpPost(myURL);//设置请求体httppost.setEntity(requesthttpEntity);//生成一个http客户端对象HttpClient httpclient = new DefaultHttpClient();InputStream inputStream = null;BufferedReader reader = null;try {//获取请求响应httpResponse = httpclient.execute(httppost);//获取数据httpEntity = httpResponse.getEntity();inputStream = httpEntity.getContent();reader = new BufferedReader(new InputStreamReader(inputStream));//保存数据String result = "";String line = "";while ((line = reader.readLine()) != null) {result += line;}// System.out.println(result);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();} finally {try {inputStream.close();reader.close();} catch (Exception e2) {// TODO: handle exception}}} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}});}


更多相关文章

  1. android 获取包名 通过包名获取应用签名
  2. Android中音频文件的使用
  3. Android(安卓)你的OnTouchEvent() 触屏事件的优化了吗
  4. [置顶] android调用.net的webservice的例子获取手机的归属地
  5. Android(安卓)反射获取私有方法,成员变量
  6. android 检测应用程序信息
  7. 获取android手机信息
  8. android 源码获取
  9. Android常用代码

随机推荐

  1. Android系列之Android 命令行手动编译打
  2. Android shell命令大全
  3. Android软键盘弹出,界面整体上移的问题
  4. android listview去掉分割线
  5. android在线播放音乐
  6. android读取文件权限
  7. Android游戏引擎《Rokon》
  8. Android TextView布局__2019.09.02
  9. Android系统修改导航栏显示位置
  10. pixel升级Android Q beta