来源: Android HttpClientConnection Get and Post – Epirus – 博客园
KeyPoint:
1.HttpClientConnection 默认是使用Get的方式的
Get方式的KeyCode
class DownloadTask extends AsyncTask<String, Integer, String>{ String resultData="lee"; @Override protected String doInBackground(String... params) { try { URL url=new URL("http://www.baidu.com"); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setDoInput(true); InputStreamReader isr=new InputStreamReader(conn.getInputStream()); BufferedReader buffer=new BufferedReader(isr); String inputLine=""; while((inputLine=buffer.readLine())!=null){ resultData+=inputLine+"\n"; } isr.close(); conn.disconnect(); System.out.println(resultData); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { textView.setText(resultData); super.onPostExecute(result); } }
这可以作为内部类放到Activity 中然后就可以直接访问UI的组件了
2.HttpClient->Post 使用Apache的,感觉非常的简洁,一个字爽
publicvoid postData(){ // Create a new HttpClient and Post Header HttpClient httpclient =newDefaultHttpClient(); HttpPost httppost =newHttpPost("http://www.yoursite.com/script.php"); try{ // Add your data List<NameValuePair> nameValuePairs =newArrayList<NameValuePair>(2); nameValuePairs.add(newBasicNameValuePair("id","12345")); nameValuePairs.add(newBasicNameValuePair("stringdata","AndDev is Cool!")); httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); //Get Data bufferedReader=new BufferedReader(new InputStreamReader(response.getEntity().getContent())); while((inputLine=bufferedReader.readLine())!=null){resultData+=inputLine+"\n";} }catch(ClientProtocolException e){ // TODO Auto-generated catch block }catch(IOException e){ // TODO Auto-generated catch block } }
如果需要使用Get方式则,设置HttpGet httpget一样的
3. HttpClientConnection POST method 官方推荐使用,其实httpclient也停止了维护,所以这个会是将来的趋势
class DownloadTask extends AsyncTask<String, Integer, String>{ String resultData="lee"; @Override protected String doInBackground(String... params) { try { URL url=new URL("http://xxxxxx.xxxx/index.php"); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.connect(); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); String content="username="+URLEncoder.encode("lee","UTF-8"); out.writeBytes(content); out.flush(); out.close(); InputStreamReader isr=new InputStreamReader(conn.getInputStream()); BufferedReader buffer=new BufferedReader(isr); String inputLine=""; while((inputLine=buffer.readLine())!=null){ resultData+=inputLine+"\n"; } isr.close(); conn.disconnect(); System.out.println(resultData); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }