Android学习笔记_HTTP

来源:互联网 发布:热血传奇挖矿脚本源码 编辑:程序博客网 时间:2024/05/22 14:33
简单例子:利用HTTP协议向客户端发送请求(eg:goole homepage)1.Get method to send requestManifest file add authority: <user-permission android:name ="android.permission.INTERNET"/>Activity类中关键代码: private HttpResponse httpResponse = null;private Button requestButton = null;private HttpEntity httpEntity = null;requestButton = (Button)findViewById(R.id.requestButton);requestButton.setOnClickListener(new OnClickListener()@overridepublic void onClick(View v){//new a HttpGet object HttpGet httpGet = new HttpGet("http://www.goole.com");//generate a Http Client objectHttpClient httpClient = new DefaultHttpClient();//use HttpClient to send request object try{httpResponse = httpClient.excute(httpGet);//I/O operation ,getContent method put the content of HttpResponse into a inputStream .httpEntity = httpResponse.getEntity();inputStream = httpEntity.getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));String result = "";String line  = "";while((line = reader.readLine()) != null){result += line;}System.out.println(result);}catch(Exception e){}finally{try{inputStream.close();}catch(Exception e){e.printStackTrace();}}});--------------------------------2.Get Method to send request with parameters: UI布局:<EditTextandroid:id = "@+id/nameView"android:layout_width = "fill_parent"android:layout_height = "wrap_content"  /><EditTextandroid:id = "@+id/ageView"android:layout_width = "fill_paent"android:layout_height = "wrap_content"  /><EditText android:id = "@+id/getButton"android:layout_width = "fill_parent"android:layout_height = "wrap_content"android:text = "use Get to send request"/><Buttonandroid:id = "@+id/postButton"android:layout_width = "fill_parent"android:layout_height = "wrap_content"android:text = "ust Post to send request"/>Manifest应用权限: <user-permission android:name="android.permission.INTERNET">Activity关键代码:  private Button getButton = null; private Button postButton = null; private EditText nameView = null; private EditText ageView = null; //cmd excute ipconfig to see SUN-PC IPv4 private String baseUrl = "http://198.168.17.1:8080/serverside/name";      ()findViewById(R.id.)   public void onClick(View v){@overrideString name  = nameView.getText.toString();String url = baseUrl + "?" +"name=" +name+ "&age=" + age;//new a HttpGet object HttpGet httpGet = new HttpGet(url);//generate a Http Client objectHttpClient httpClient = new DefaultHttpClient();//use HttpClient to send request object try{httpResponse = httpClient.excute(httpGet);//I/O operation ,getContent method put the content of HttpResponse into a inputStream .httpEntity = httpResponse.getEntity();inputStream = httpEntity.getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));String result = "";String line  = "";while((line = reader.readLine()) != null){result += line;} }  -------------------------------------3.POST method to send request with parameters .  postButton.setOnClickListener(new OnClickListener(){@overridepublic void onClick(View v){String name = nameView.getText().toString();String age = ageView.getText().toString();NameValuePair nameValuePair1 = new BasicNameValuePair("name",name);NameValuePair nameValuePair2 = new BasicNameValuePair("age",age);List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>{};    nameVlauePairs.add(nameValuePair1);nameValuePairs.add(nameValuePair2);try{HttpEntity requestHttpEntity = new UrlEncodeFormEntity(nameValuePairs);HttpPost httpPost = new HttpPost(baseUrl);httpPost.setEntity(requestHttpEntity);HttpClient httpClient = new DefaultHttpClient();InputStream inputStream = null;try{httpResponse = httpClient.excute(httpPost);//I/O operation ,getContent method put the content of HttpResponse into a inputStream .httpEntity = httpResponse.getEntity();inputStream = httpEntity.getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));String result = "";String line  = "";while((line = reader.readLine()) != null){result += line;}System.out.println(result);}catch(Exception e){}finally{try{inputStream.close();}catch(Exception e){e.printStackTrace();}}}catch(Exception e){e.printStackTrace();}}  });