Android平台Google Reader API开发问题
我想利用Goole Reader API来获得用户订阅的列表。根据Goole Reader API操作。
第一步POST,将用户名和密码发送给https://www.google.com/accounts/ClientLogin没有问题,正确得到SID。
第二部利用SID作为Cookie向http://www.google.com/reader/atom/发起GET请求却遇到的问题,不能得到想要的XML文件,只能得到一个未登录的HTML页面文件,我明明在第一步登录了啊。
附代码(为了保护隐私,其中用户名和密码用*表示):
package hello.android;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloAndroid extends Activity {
Button btn;
TextView tv;
EditText et;
OnClickListener btnClick;
String strResult;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)this.findViewById(R.id.textview1);
if (tv == null) {
return;
}
btn = (Button)this.findViewById(R.id.button1);
if (btn == null)
return;
et = (EditText)this.findViewById(R.id.edittext1);
if (et == null)
return;
btnClick = new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("test");
googleReader();
}
};
btn.setOnClickListener(btnClick);
}
public boolean httpPost(String url, List<NameValuePair> params) {
boolean ret = true;
HttpPost httpRequest = new HttpPost(url);
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
strResult = EntityUtils.toString(httpResponse.getEntity());
et.setText(strResult);
}
else {
ret = false;
et.setText("Error Response: " + httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e) {
ret = false;
et.setText(e.getMessage().toString());
}
catch (IOException e) {
ret = false;
et.setText(e.getMessage().toString());
}
catch (Exception e) {
ret = false;
et.setText(e.getMessage().toString());
}
return ret;
}
public boolean httpGet(String url, List<NameValuePair> params) {
boolean ret = true;
HttpGet httpRequest = new HttpGet(url);
for (int i = 0; i < params.size(); ++i) {
httpRequest.addHeader(params.get(i).getName(), params.get(i).getValue());
}
try {
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String strResult = EntityUtils.toString(httpResponse.getEntity());
et.setText(strResult);
}
else {
et.setText("Error Response: " + httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e) {
ret = false;
et.setText(e.getMessage().toString());
}
catch (IOException e) {
ret = false;
et.setText(e.getMessage().toString());
}
catch (Exception e) {
ret = false;
et.setText(e.getMessage().toString());
}
return ret;
}
public boolean googleReader() {
String url = "https://www.google.com/accounts/ClientLogin";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("service", "reader"));
params.add(new BasicNameValuePair("Email", "******@gmail.com"));
params.add(new BasicNameValuePair("Passwd", "*******"));
params.add(new BasicNameValuePair("continue", "http://www.google.com/"));
// 发送POST请求,获得一串文本
if (!httpPost(url, params))
return false;
// 解析SID
int startSID = strResult.indexOf("SID=");
if (startSID == -1)
return false;
int endSID = strResult.indexOf("\n", startSID);
if (endSID == -1)
return false;
// 得到SID
String strSID = strResult.substring(startSID + 4, endSID);
url = "http://www.google.com/reader/atom/";
params.clear();
// 构造Cookies
String strCookies = "name=";
strCookies += (strSID + "; ");
strCookies += "domain=.google.com; ";
strCookies += "path=/; ";
strCookies += "expires=1600000000";
params.add(new BasicNameValuePair("Cookie", strCookies));
// 发送GET请求
if (!httpGet(url, params))
return false;
return true;
}
}
补充:移动开发 , Android