編輯:關於Android編程
首先是Get:
布局很簡單,就一個按鈕,主要看一下MainActivity吧:
package com.francis.httpget; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MyActivity extends Activity { BufferedReader bufferedReader; InputStreamReader inputStreamReader; InputStream inputStream; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AsyncTask() { @Override protected Void doInBackground(String... params) { try { URL url = new URL(params[0]); URLConnection connection = url.openConnection(); inputStream = connection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream,"UTF-8"); bufferedReader = new BufferedReader(inputStreamReader); String line; while (true) { if ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } } catch (Exception e) { e.printStackTrace(); } finally { try { bufferedReader.close(); inputStreamReader.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }.execute("http://fanyi.youdao.com/openapi.do?keyfrom=HttpGetFrancis&key=893755706&type=data&doctype=xml&version=1.1&q=這裡是有道翻譯API"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
接著是Post:
很簡單,這裡只給出提交post請求的Button的點擊事件:
和Get相比有2點需要注意:
1.url與參數是分開的;<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Mi5jb25uZWN0aW9uLnNldERvSW5wdXQodHJ1ZSk7PGJyPgogICBjb25uZWN0aW9uLnNldERvT3V0cHV0KHRydWUpOzxicj4KICAgY29ubmVjdGlvbi5zZXRSZXF1ZXN0TWV0aG9kKA=="POST");
// Post findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AsyncTask() { @Override protected Void doInBackground(String... params) { try { URL url = new URL(params[0]); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); outputStream = connection.getOutputStream(); outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8"); bufferedWriter = new BufferedWriter(outputStreamWriter); bufferedWriter.write("keyfrom=HttpGetFrancis&key=893755706&type=data&doctype=xml&version=1.1&q=這裡是有道翻譯API"); bufferedWriter.flush(); inputStream = connection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream,"UTF-8"); bufferedReader = new BufferedReader(inputStreamReader); String line; while (true) { if ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } } catch (Exception e) { e.printStackTrace(); } finally { try { bufferedReader.close(); inputStreamReader.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }.execute("http://fanyi.youdao.com/openapi.do"); } });
本文實例為大家分享了TextView繪制背景的方法,供大家參考,具體內容如下效果:實現流程:1.初始化:對畫筆進行設置mPaintIn = new Paint();mPa
群英傳的最後一章,我大致的看了一下這個例子,發現鴻洋大神也做過,就參考兩個人的設計邏輯,感覺都差不多,就這樣實現起來了一.切圖工具類 我們九宮格嘛,肯定要一
在開始講述touch事件流程之前,還簡單介紹下TouchEvent,View和ViewGroup。1. MotionEvent 整個事件分發流程中,會
AppWidget通常被稱為桌面插件,是Android系統應用開發層上的重要部分,是把一個進程的控件嵌入到別外一個進程的窗口裡的一種方法,是與客戶端程序交互的組件程序。