0

好的...在我寻求在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库

4

2 回答 2

1

最好的开始方法是使用wireshark 并查看浏览器在发布状态时正在做什么。然后在 HTTPClient 中进行适配。

于 2012-06-01T01:59:59.993 回答
0

在 Mobile Client 中保持会话的最佳方式应该是使用 OAuth,当您获得 OAuth 令牌时,您可以使用令牌来使用 Facebook 提供的任何 api。

于 2012-06-01T03:29:46.187 回答