好的...在我寻求在android中以编程方式上网的过程中。我终于确信 apache http api 是要走的路。我需要一些帮助来使用它。我已经设法使用 httppost 登录,但是我希望之后能够继续。再次以 facebook 为例,我登录到 facebook,现在我想使用这个登录会话来发布一个状态,我该如何继续?这是到目前为止的代码(需要清理和东西,它主要是从各种来源复制的)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("https://www.facebook.com/");
try{
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
// System.out.println("Login form get: " + response.getStatusLine());
// EntityUtils.consume(entity);
// System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
// https://www.facebook.com/login.php?login_attempt=1
HttpPost httpost = new HttpPost("https://www.facebook.com/login.php?login_attempt=1");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("email", "****"));
nvps.add(new BasicNameValuePair("pass", "****"));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
entity = response.getEntity();
Toast.makeText(getBaseContext(), "done",Toast.LENGTH_LONG);
System.out.println("Login form get: " + response.getStatusLine());
// EntityUtils.consume(entity);
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
}finally {} }catch (Exception e) {
// TODO: handle exception
}finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
Toast.makeText(getBaseContext(), "done",Toast.LENGTH_LONG);
}
}
}
用您的电子邮件和密码替换星星,这会让您进入 Facebook,我如何从那里继续发布状态?
再次请不要告诉我有一个facebook api,我不想使用它,这里的重点是学习使用apache库