貧窮不能等,因為時間久了,你就會貧窮習慣了;
夢想不能等,因為努力晚了,人老就無能為力了;
學習不能等,因為懂得少了,就沒本事夢想成真了;
健康不能等,因為身體垮了,人生的一切就都沒了。
- 你不能決定生命的長度,但可以控制它的寬度;
- 你不能左右天氣,但可以改變心情;
- 你不能改變容顏,但可以展現笑容;
- 你不能控制他人,但可以掌握自己;
- 你不能預知明天,但可以利用今天;
- 你不能樣樣勝利,但可以事事盡力。
- Sep 19 Fri 2014 01:37
-
OS X 安裝 ant 指令
- May 21 Wed 2014 19:28
-
[轉貼] Load a POST request into a WebView in Android
- May 21 Wed 2014 16:39
-
[轉貼] Android Programming: Send data via POST to WebView (e.g. to implement an automatic login)
With the following code, it is possible to send data to a WebView using POST with Android:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WebView wv = new WebView(this); setContentView(wv); String data = "name=Test&password=12345"; wv.postUrl(url, EncodingUtils.getBytes(data, "base64"));}- May 21 Wed 2014 16:14
-
[轉貼] Call an activity method from a fragment
Q:
Trying to call a method in my activity from a fragment. I want the fragment to give the method data and to get the data when the method return. I want to achieve similar to call on a static method, but without the use of static because it create problems in the activity.
New to fragments so I need an easy and pedagogic explanation!
Thanks!
Trying to call a method in my activity from a fragment. I want the fragment to give the method data and to get the data when the method return. I want to achieve similar to call on a static method, but without the use of static because it create problems in the activity.
New to fragments so I need an easy and pedagogic explanation!
Thanks!
- May 14 Wed 2014 12:29
-
[轉] Android-Bootstrap
做到一种样式 android 的spinner本身背景样式要黑色的 ,而spinner里的字体也是黑色的 ,spinner 控件的属性里设置TextColor没用 网上找了下 需要定义xml 文件来定义样式 和自定义按钮控件类似;
1,首先定义spinenr本身背景的样式:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/droplist_down" />
<item android:state_focused="true" android:drawable="@drawable/droplist_focus" />
<item android:drawable="@drawable/droplist" />
</selector>
1,首先定义spinenr本身背景的样式:
- May 13 Tue 2014 16:36
-
[轉] Android AppTheme 系統預設樣式android:Theme語法整理
建立Android Project後會多了res/values/styles.xml的樣式檔案
事實上這是讓Android讀取的預設的樣式定義檔
例如要讓整個程式的基本樣式為亮色系或暗色系~
或者要隱藏標題全螢幕等等, 皆可以在這個檔案上進行修改...
事實上這是讓Android讀取的預設的樣式定義檔
例如要讓整個程式的基本樣式為亮色系或暗色系~
或者要隱藏標題全螢幕等等, 皆可以在這個檔案上進行修改...
- Mar 31 Mon 2014 00:40
-
[轉帖] 在Android中使用顏色的方法
- Dec 21 Sat 2013 04:02
-
Convert String to Uri
You can use the
parse static method from UriUri myUri =Uri.parse("http://stackoverflow.com")- Dec 21 Sat 2013 02:47
-
[轉貼] 一個可以接web service回應的Android HTTP client
在瀏覽器裡頭有實做AJAX query,這讓網頁有了更大的發揮空間,
程式人員可以讓網頁就像一般應用程式一樣有趣。
那如果,一般應用程式也可以發出HTTP query,
進而取得web service吐出的東西,那又會擦出怎樣的火花呢? 先賣個關子~
本篇只講在Android上,要如何寫一個發出HTTP query,又接回HTTP response的程式。
關鍵字:Android & HttpClient
在Android SDK裡有引入org.apache.http.client,
這package是這程式的靈魂,詳情可以看本篇的參考。
底下是程式:
流程:
1.建一個text view
2.呼叫HTTPGetQuery
2.1.建一個HTTP client
2.2.建一個HTTP get物件,並在初始化的參數帶入URL
2.3.建一個HTTP reponse handler物件
2.4.HTTP client實際對web service query,並帶入HTTP get
2.5.取回reponse,再從response取出status code、header、cookie、content字串
2.6.因為HTTP是socket IO,所以用try catch包起來,以防IO錯誤時沒有處理到
2.7.回傳結果
3.將結果放到text view
4.畫出這個程式畫面
程式碼:
package androidajax.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class AndroidAJAX extends Activity {
private Document xmldoc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Have a text view on the scene.
TextView tv = new TextView(this);
// Put the answer string returned by the HTTP query on the scene.
// Change the red url by yourself.
Map map = HTTPGetQuery("http://test.com.url?account=牛蛙");
//Map map = HTTPPostQuery("http://test.com.url");
tv.setText(map.get("status").toString() + ": " + map.get("content").toString());
// Render the scene.
setContentView(tv);
}
/** Method: Do the HTTP query according to hostURL with "get" method. */
private Map<String, String> HTTPGetQuery(String hostURL) {
// Declare a content string prepared for returning.
String content = "";
// Have an HTTP client to connect to the web service.
HttpClient httpClient = new DefaultHttpClient();
// Have an HTTP response container.
HttpResponse httpResponse = null;
// Have map container to store the information.
Map<String, String> map = new HashMap<String, String>();
// This try & catch is prepared for the IO exception in case.
try {
// Have a post method class with the query URL.
HttpGet httpQuery = new HttpGet(hostURL);
// The HTTP client do the query and have the string type response.
httpResponse = httpClient.execute(httpQuery);
// Read the HTTP headers and into content.
//for (Header header : httpResponse.getAllHeaders()) {
// content += "\n" + header.toString();
//}
// Read the HTTP response content as an encoded string.
content += EntityUtils.toString(httpResponse.getEntity());
}
// Catch the HTTP exception.
catch(ClientProtocolException ex) {
content = "ClientProtocolException:" + ex.getMessage();
}
// Catch the any IO exception.
catch(IOException ex) {
content = "IOException:" + ex.getMessage();
}
// The HTTP connection must be closed any way.
finally {
httpClient.getConnectionManager().shutdown();
}
// Check the HTTP connection is executed or not.
if (httpResponse != null) {
// Put the status code with status key.
map.put("status", Integer.toString(httpResponse.getStatusLine().getStatusCode()));
// Put the response content with content key
map.put("content", content);
}
else {
// Put the dummy with status key.
map.put("status", "");
// Put the dummy with content key
map.put("content", "");
}
// Return result.
return map;
}
/** Method: Do the HTTP query according to hostURL with "post" method. */
private Map<String, String> HTTPPostQuery(String hostURL) {
// Declare a content string prepared for returning.
String content = "";
// Have an HTTP client to connect to the web service.
HttpClient httpClient = new DefaultHttpClient();
// Have an HTTP response container.
HttpResponse httpResponse = null;
// Have map container to store the information.
Map<String, String> map = new HashMap<String, String>();
// This try & catch is prepared for the IO exception in case.
try {
// Have a post method class with the query URL.
HttpPost httpQuery = new HttpPost(hostURL);
// Have a list of key-value pair container.
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
// Add the HTTP post arguments into the list.
pairs.add(new BasicNameValuePair("account", "青蛙"));
pairs.add(new BasicNameValuePair("password", "frog"));
// Assign the list as the arguments of post being UTF_8 encoding.
httpQuery.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
// The HTTP client do the query and have the string type response.
httpResponse = httpClient.execute(httpQuery);
// Read the HTTP response content as an encoded string.
content += EntityUtils.toString(httpResponse.getEntity());
}
// Catch the HTTP exception.
catch(ClientProtocolException ex) {
content = "ClientProtocolException:" + ex.getMessage();
}
// Catch the any IO exception.
catch(IOException ex) {
content = "IOException:" + ex.getMessage();
}
// The HTTP connection must be closed any way.
finally {
httpClient.getConnectionManager().shutdown();
}
// Check the HTTP connection is executed or not.
if (httpResponse != null) {
// Put the status code with status key.
map.put("status", Integer.toString(httpResponse.getStatusLine().getStatusCode()));
// Put the response content with content key.
map.put("content", content);
}
else {
// Put the dummy with status key.
map.put("status", "");
// Put the dummy with content key.
map.put("content", "");
}
// Return result.
return map;
}
}
因為用到HTTP client,這會需要用到網路IO,需要權限授權,
所以要改 res/AndroidManifest.xml
我在</APPLICATION>下加上:
<uses-permission android:name="android.permission.INTERNET" />
參考:
Android 上的 HTTP 服務相關函式:
http://ysl-paradise.blogspot.com/2008/09/android-http-i.html
http://ysl-paradise.blogspot.com/2008/09/android-http-ii.html
http://ysl-paradise.blogspot.com/2008/09/android-http-iii.html
HttpClient Examples:
http://hc.apache.org/httpcomponents-client-ga/examples.html
http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientWithResponseHandler.java
Permission denied error while call webservice using HTTP in android:
http://stackoverflow.com/questions/3932129/permission-denied-error-while-call-webservice-using-http-in-android-application
本文取自 http://blog.xuite.net/zack_pan/blog/47887919
- Dec 21 Sat 2013 02:13
-
[轉貼] 禁止屏幕随手机旋转变化 及 避免在转屏时重启Activity
禁止屏幕随手机旋转变化
有时候我们希望让一个程序的界面始终保持在一个方向,不随手机方向旋转而变化:
在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入 android:screenOrientation=”landscape” 属性。
1