当前位置: 编程技术>移动开发
本页文章导读:
▪http协议范例 http协议实例
1. 通过 GET 请求获取数据
/**
* Sends an HTTP GET request to a url
* @param endpoint
* - The URL of the server. (Example:
* " http://www.yahoo.com/search")
* @param requestParameters
* - all the request parameters (Exampl.........
▪ Objective-C属性引见 Objective-C属性介绍
我们知道在Objective-C中,使用@property配合@synthesize可以让编译器自动实现getter/setter方法,使用的时候也很方便,可以直接使用对象.属性的方法调用。NSString* name;NSUInte.........
▪ ListView平添事件并获取选中项的值 ListView添加事件并获取选中项的值
http://www.beijibear.com/?aid=341为什么ListView.setOnItemClickListener、setOnCreateContextMenuListener会无效:http://blog.csdn.net/mahaiyun18/article/details/6531371如果ListView中的单个Ite.........
[1]http协议范例
来源: 互联网 发布时间: 2014-02-18
http协议实例
1. 通过 GET 请求获取数据
/**
* Sends an HTTP GET request to a url
* @param endpoint
* - The URL of the server. (Example:
* " http://www.yahoo.com/search")
* @param requestParameters
* - all the request parameters (Example:
* "param1=val1¶m2=val2"). Note: This method will add the
* question mark (?) to the request - DO NOT add it yourself
* @return - The response from the end point
*/
public static String sendGetRequest(String endpoint,
String requestParameters) {
Log.i("sendGetRequest", endpoint);
String result = null;
if (endpoint.startsWith("http://")) {
// Send a GET request to the servlet
try {
// Construct data
StringBuffer data = new StringBuffer();
// Send data
String urlStr = endpoint;
if (requestParameters != null && requestParameters.length() > 0) {
urlStr += "?" + requestParameters;
}
Log.i("urlStr", urlStr);
URL url = new URL(/blog_article/urlStr/index.html);
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}}
Log.i("sendGetRequest", result);
return result;
}
2. 获取网上图片并转换格式
public static Bitmap returnBitMap(String url) {
Log.i("returnBitMap", "url=" + url);
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(/blog_article/url/index.html);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
3. 通过 POST 提交数据
public void MyFunction{
HttpClient httpclient = new DefaultHttpClient();
// 你的 URL
HttpPost httppost = new HttpPost("http://www.eoeandroid.com/post_datas.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//Your DATA
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "eoeAndroid.com is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response;
response=httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
[2] Objective-C属性引见
来源: 互联网 发布时间: 2014-02-18
Objective-C属性介绍
我们知道在Objective-C中,使用@property配合@synthesize可以让编译器自动实现getter/setter方法,使用的时候也很方便,可以直接使用对象.属性的方法调用。
NSString* name;
NSUInteger age;
@property(nonatomic,copy)NSString* name;
@property(assign)NSUInteger age;
@synthesize name;
@synthesize age;
复制代码
那如果我们想要对象.方法的方式来调用一个方法并获取到方法的返回值,那就需要使用@property配合@dynamic了。
@property(readonly)NSString* firstArrayValue;
@dynamic firstArrayValue;
- (NSString*)firstArrayValue
{
return [_array objectAtIndex:0];
}
复制代码
这样就可以使用对象.firstArrayValue来获取到_array数组中的第一个值了,很显然,这种方法并不适用于需要传递参数的方法。
其实使用@dynamic关键字是告诉编译器由我们自己来实现访问方法。
如果使用的是@synthesize,那么这个工作编译器就会帮你实现了。
说明:代码只为示例代码,实际使用时每句代码要放到相应位置的。
===== 最后转载下关于@property(*)括号中的属性内容介绍 =====
readonly
此标记说明属性是只读的,默认的标记是读写,如果你指定了只读,在@implementation中只需要一个读取器。或者如果你使用@synthesize关键字,也是有读取器方法被解析。而且如果你试图使用点操作符为属性赋值,你将得到一个编译错误。
readwrite
此标记说明属性会被当成读写的,这也是默认属性。设置器和读取器都需要在@implementation中实现。如果使用@synthesize关键字,读取器和设置器都会被解析。
assign
此标记说明设置器直接进行赋值,这也是默认值。在使用垃圾收集的应用程序中,如果你要一个属性使用assign,且这个类符合NSCopying协议,你就要明确指出这个标记,而不是简单地使用默认值,否则的话,你将得到一个编译警告。这再次向编译器说明你确实需要赋值,即使它是可拷贝的。
retain
指定retain会在赋值时唤醒传入值的retain消息。此属性只能用于Objective-C对象类型,而不能用于Core Foundation对象。(原因很明显,retain会增加对象的引用计数,而基本数据类型或者Core Foundation对象都没有引用计数——译者注)。
copy
它指出,在赋值时使用传入值的一份拷贝。拷贝工作由copy方法执行,此属性只对那些实行了NSCopying协议的对象类型有效。更深入的讨论,请参考“复制”部分。
nonatomic
指出访问器不是原子操作,而默认地,访问器是原子操作。这也就是说,在多线程环境下,解析的访问器提供一个对属性的安全访问,从获取器得到的返回值或者通过设置器设置的值可以一次完成,即便是别的线程也正在对其进行访问。如果你不指定nonatomic,在自己管理内存的环境中,解析的访问器保留并自动释放返回的值,如果指定了nonatomic,那么访问器只是简单地返回这个值。
我们知道在Objective-C中,使用@property配合@synthesize可以让编译器自动实现getter/setter方法,使用的时候也很方便,可以直接使用对象.属性的方法调用。
NSString* name;
NSUInteger age;
@property(nonatomic,copy)NSString* name;
@property(assign)NSUInteger age;
@synthesize name;
@synthesize age;
复制代码
那如果我们想要对象.方法的方式来调用一个方法并获取到方法的返回值,那就需要使用@property配合@dynamic了。
@property(readonly)NSString* firstArrayValue;
@dynamic firstArrayValue;
- (NSString*)firstArrayValue
{
return [_array objectAtIndex:0];
}
复制代码
这样就可以使用对象.firstArrayValue来获取到_array数组中的第一个值了,很显然,这种方法并不适用于需要传递参数的方法。
其实使用@dynamic关键字是告诉编译器由我们自己来实现访问方法。
如果使用的是@synthesize,那么这个工作编译器就会帮你实现了。
说明:代码只为示例代码,实际使用时每句代码要放到相应位置的。
===== 最后转载下关于@property(*)括号中的属性内容介绍 =====
readonly
此标记说明属性是只读的,默认的标记是读写,如果你指定了只读,在@implementation中只需要一个读取器。或者如果你使用@synthesize关键字,也是有读取器方法被解析。而且如果你试图使用点操作符为属性赋值,你将得到一个编译错误。
readwrite
此标记说明属性会被当成读写的,这也是默认属性。设置器和读取器都需要在@implementation中实现。如果使用@synthesize关键字,读取器和设置器都会被解析。
assign
此标记说明设置器直接进行赋值,这也是默认值。在使用垃圾收集的应用程序中,如果你要一个属性使用assign,且这个类符合NSCopying协议,你就要明确指出这个标记,而不是简单地使用默认值,否则的话,你将得到一个编译警告。这再次向编译器说明你确实需要赋值,即使它是可拷贝的。
retain
指定retain会在赋值时唤醒传入值的retain消息。此属性只能用于Objective-C对象类型,而不能用于Core Foundation对象。(原因很明显,retain会增加对象的引用计数,而基本数据类型或者Core Foundation对象都没有引用计数——译者注)。
copy
它指出,在赋值时使用传入值的一份拷贝。拷贝工作由copy方法执行,此属性只对那些实行了NSCopying协议的对象类型有效。更深入的讨论,请参考“复制”部分。
nonatomic
指出访问器不是原子操作,而默认地,访问器是原子操作。这也就是说,在多线程环境下,解析的访问器提供一个对属性的安全访问,从获取器得到的返回值或者通过设置器设置的值可以一次完成,即便是别的线程也正在对其进行访问。如果你不指定nonatomic,在自己管理内存的环境中,解析的访问器保留并自动释放返回的值,如果指定了nonatomic,那么访问器只是简单地返回这个值。
[3] ListView平添事件并获取选中项的值
来源: 互联网 发布时间: 2014-02-18
ListView添加事件并获取选中项的值
http://www.beijibear.com/?aid=341
为什么ListView.setOnItemClickListener、setOnCreateContextMenuListener会无效:
http://blog.csdn.net/mahaiyun18/article/details/6531371
如果ListView中的单个Item的view中存在checkbox,button等view,会导致ListView.setOnItemClickListener无效,
事件会被子View捕获到,ListView无法捕获处理该事件.
解决方法:
在checkbox、button对应的view处加android:focusable="false"
android:clickable="false" android:focusableInTouchMode="false"
其中focusable是关键
Android ListView/ListActivity点击长按事件:
http://blog.163.com/zmhot88@126/blog/static/16984664720107319215488/
ListActivity和ListView是很常用的组件,用来制作列表形式的用户界面。本文介绍如何正确处理ListView中的条目短按和长 按事件,他们的处理方式是不同的。
对于短按事件,处理起来比较简单,我们只需要覆盖ListActivity的onListItemClick()方法,如下所示:
@Override
protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3) {
CharSequence s = ((TextView)arg1).getText();
Log.e("CallLogActivity",s+ " is clicked");
super.onListItemClick(arg0, arg1, arg2, arg3);
}
对于长按事件,我们需要给listview注册一个OnItemLongClickListener,并实现Listener中定义的方法,如 下所示:
下面是例子代码。不包含上述技术
--------------------------------------------
main.xml代码如下:
list_item.xml代码如下:
activity MyListView.java代码如下:
http://www.beijibear.com/?aid=341
为什么ListView.setOnItemClickListener、setOnCreateContextMenuListener会无效:
http://blog.csdn.net/mahaiyun18/article/details/6531371
如果ListView中的单个Item的view中存在checkbox,button等view,会导致ListView.setOnItemClickListener无效,
事件会被子View捕获到,ListView无法捕获处理该事件.
解决方法:
在checkbox、button对应的view处加android:focusable="false"
android:clickable="false" android:focusableInTouchMode="false"
其中focusable是关键
Android ListView/ListActivity点击长按事件:
http://blog.163.com/zmhot88@126/blog/static/16984664720107319215488/
ListActivity和ListView是很常用的组件,用来制作列表形式的用户界面。本文介绍如何正确处理ListView中的条目短按和长 按事件,他们的处理方式是不同的。
对于短按事件,处理起来比较简单,我们只需要覆盖ListActivity的onListItemClick()方法,如下所示:
@Override
protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3) {
CharSequence s = ((TextView)arg1).getText();
Log.e("CallLogActivity",s+ " is clicked");
super.onListItemClick(arg0, arg1, arg2, arg3);
}
对于长按事件,我们需要给listview注册一个OnItemLongClickListener,并实现Listener中定义的方法,如 下所示:
getListView().setOnItemLongClickListener(new OnItemLongCLickListener(){
public boolean onItemLongClick(AdapterView parent, View view, int position,
long id) {
Log.e("CallLogActivity", view.toString() + "position=" + position);
CharSequence number = ((TextView) view).getText();
Toast t = Toast.makeText(this, number + " is long clicked",
Toast.LENGTH_LONG);
t.show();
return true;
}
});下面是例子代码。不包含上述技术
--------------------------------------------
main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout> list_item.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dip"
android:paddingRight="12dip"
/>
<TextView
android:id="@+id/itemContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dip"
/>
</LinearLayout> activity MyListView.java代码如下:
package listview.pack;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MyListView extends Activity {
/** Called when the activity is first created. */
//声明ListView对象
ListView myListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//生成ListView对象
myListView=(ListView)findViewById(R.id.myListView);
//创建ArrayList对象 并添加数据
ArrayList<HashMap<String,String>> myArrayList=new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("itemTitle", "This Is Title "+i);
map.put("itemContent", "This Is Content "+i);
myArrayList.add(map);
}
//生成SimpleAdapter适配器对象
SimpleAdapter mySimpleAdapter=new SimpleAdapter(this,
myArrayList,//数据源
R.layout.list_items,//ListView内部数据展示形式的布局文件listitem.xml
new String[]{"itemTitle","itemContent"},//HashMap中的两个key值 itemTitle和itemContent
new int[]{R.id.itemTitle,R.id.itemContent});/*布局文件listitem.xml中组件的id
布局文件的各组件分别映射到HashMap的各元素上,完成适配*/
myListView.setAdapter(mySimpleAdapter);
//添加点击事件
myListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//获得选中项的HashMap对象
HashMap<String,String> map=(HashMap<String,String>)myListView.getItemAtPosition(arg2);
String title=map.get("itemTitle");
String content=map.get("itemContent");
Toast.makeText(getApplicationContext(),
"你选择了第"+arg2+"个Item,itemTitle的值是:"+title+"itemContent的值是:"+content,
Toast.LENGTH_SHORT).show();
}
});
}
}
最新技术文章: