我有一个要通过意图发送到服务类的类(下).我已经实现了Parcelable接口,但是不确定如何实际发送和检索包括对象当前状态在内的整个对象.
尤其是
@Override
public void writeToParcel(Parcel dest, int flags) {
//I need this to send the entire state of the object
}
和
public UrlParamsHelper(Parcel in) {
//I need this to unpack the state of the object
}
这是实际的课程
/*
* A holder class for URL parameters
*/
public static class UrlParamsHelper implements Parcelable {
private final HttpClient httpClient;
private final HttpParams params = new BasicHttpParams();
private final SchemeRegistry registry = new SchemeRegistry();
private final ThreadSafeClientConnManager manager;
private final Uri.Builder uri = new Uri.Builder();
final HttpHost host;
final String urlPath;
final String hostname;
/*
* @param hostname the hostname ie. http://www.google.com
* @param urlPath the path to the file of interest ie. /getfiles.php
*/
public UrlParamsHelper(final String hostname, final String urlPath) {
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80));
manager = new ThreadSafeClientConnManager(params, registry);
httpClient = new DefaultHttpClient(manager, params);
host = new HttpHost(hostname, 80, "http");
uri.path(urlPath);
this.urlPath = urlPath;
this.hostname = hostname;
}
public UrlParamsHelper(Parcel in) {
//unpack the state
}
public void addQueryString(String key, String value) {
uri.appendQueryParameter(key, value);
}
public HttpGet buildGetQuery() {
return new HttpGet(uri.build().toString());
}
public HttpClient getHttpClient() {
return httpClient;
}
public HttpHost getHttpHost() {
return host;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//Parcel the entire state of the object
}
//Constructs the parcel again - REQUIRED
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public UrlParamsHelper createFromParcel(Parcel in) {
return new UrlParamsHelper(in);
}
public UrlParamsHelper[] newArray(int size) {
throw new UnsupportedOperationException();
//return new UrlParamsHelper[size];
}
};
}