編輯:關於Android編程
GET請求Demo:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textViewShow = (TextView) findViewById(R.id.showText); //直接在URL後添加請求參數 String url = "http://192.168.1.103/index.php?get1=hello&get2=bay"; try { // 創建DefaultHttpClient對象 HttpClient httpclient = new DefaultHttpClient(); // 創建一個HttpGet對象 HttpGet get = new HttpGet(url); // 獲取HttpResponse對象 HttpResponse response = httpclient.execute(get); //判斷是否鏈接成功 if (response.getStatusLine().getStatusCode() == 200) { //實體轉換為字符串 String content = EntityUtils.toString(response.getEntity(),"utf-8"); textViewShow.setText(content); }else{ textViewShow.setText("網絡錯誤"); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }POST請求Demo:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textViewShow = (TextView) findViewById(R.id.showText); String url = "http://192.168.1.103/index.php"; HttpClient httpClient = new DefaultHttpClient(); try { HttpPost post = new HttpPost(url); List params = new ArrayList(); params.add(new BasicNameValuePair("get1", "hello")); params.add(new BasicNameValuePair("get2", "usrl")); post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse response = httpClient.execute(post); if(response.getStatusLine().getStatusCode() ==200){ String content = EntityUtils.toString(response.getEntity(),"utf-8"); textViewShow.setText(content); }else{ textViewShow.setText("網絡問題"); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block textViewShow.setText("UnsupportedEncodingException"); } catch (ClientProtocolException e) { // TODO Auto-generated catch block textViewShow.setText("ClientProtocolException"); } catch (IOException e) { // TODO Auto-generated catch block textViewShow.setText("IOException"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Android 仿微信 圖片選擇器采用項目的框架上采用mvp模式,並用了我自己寫的一套lib,地址ndileber首先先定義了數據源,數據源可能來自於網絡也可能來自於本地
我在此基礎上優化了部分代碼, 添加了滑動回調, 可自定義性更強. 並且添加了點擊按鈕左右滑動的功能.據說無圖都不敢發文章了.看圖:1:這種功能, 首先需要自己管理布局繼承
小編也曾經遇到過連續幾天都打不開新浪微博的圖片,一開始還以為新浪微博的服務器出問題了!於是問問朋友們有沒這種問題,然而他們都沒出現。於是小編就自己研究了一下
在網絡信息高速發展的今天,移動設備的方便快捷已經深入人心,越來越多的開發人員會選擇在移動設備上查看或編輯源代碼。於是,Android平台上大量基於代碼編程的應用應運而生,