2011-03-02 7 views
2

私の要件はthis questionと非常によく似ています。Androidアプリケーションでウェブコンテンツ(POST/GET)にアクセスする方法は?

基本的に私はアンドロイドアプリでログインアクティビティを使用しています。ユーザがデータを入力してログインをクリックすると、自分のウェブサイトにアクセスし、ユーザーを認証し、結果を取得し、か否か。

ここに私の質問があります。

  1. 私は上記のアンドロイドを実装するためのオプションは何ですか?データをPOSTして結果を自分のアクティビティに戻すにはどうすればよいですか?
  2. WebViewを使用している場合は、これを簡略化できますか?
+0

はただ、他の2つの答えに追加する:あなたはWebViewのを使用する場合、プレーン*ウェブ*アプリになるだろう。それ以外の場合は、AsyncTaskを使用してUIスレッドをブロックしないようにしてください。 – bigstones

答えて

4

あなたはHttpClientとURIに投稿することができます。

URI uri = URI.create("http://whatever.com/thingie"); 
HttpPost post = new HttpPost(uri); 
StringEntity ent = new StringEntity("Here is my data!"); 
post.setEntity(ent); 
HttpClient httpClient = new DefaultHttpClient(); 
HttpResponse response = httpClient.execute(request); 

あなたが見てする必要があるすべてのものがパッケージorg.apache.http.clientです。そこにあなたのためにインターネット上でさらに多くの例があります。

1

HttpClientです。オープンソースライブラリDroidFuは、HttpClientを効率的に使用する方法の素晴らしい例です。あなたはそれを見つけることができますhere

1

は私がサンプルコード(ここではSOの私の前の回答の1からのサンプルコード)を使用してお見せしましょう:

public CookieStore sendPostData(String url, String user, String pass) { 

    // Setup a HTTP client, HttpPost (that contains data you wanna send) and 
    // a HttpResponse that gonna catch a response. 
    DefaultHttpClient postClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(url); 
    HttpResponse response; 

    try { 

     // Make a List. Increase the size as you wish. 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 

     // Add your form name and a text that belongs to the actual form. 
     nameValuePairs.add(new BasicNameValuePair("username_form", user)); 
     nameValuePairs.add(new BasicNameValuePair("password_form", pass)); 

     // Set the entity of your HttpPost. 
     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute your request against the given url and catch the response. 
     response = postClient.execute(httpPost); 

     // Status code 200 == successfully posted data. 
     if(response.getStatusLine().getStatusCode() == 200) { 
     // Green light. Catch your cookies from your HTTP response. 
     CookieStore cookies = postClient.getCookieStore(); 
     return cookies; 
     } 
    } catch (Exception e) { 
    } 
} 

は、今すぐあなたのクッキーを設定する(またはチェック/それらを検証)する必要があり、あなたに対して要求を行う前に、サーバ。

サンプルコード:

CookieStore cookieStore = sendPostData("www.mypage.com/login", "Username", 
              "Password"); 

// Note, you may get more than one cookie, therefore this list. 
List<Cookie> cookie = cookieStore.getCookies(); 

// Grab the name of your cookie. 
String cookieOne = cookie.get(0).getName(); 

あなたが本当に行う必要が何なWiresharkとして有益なツールを使用して、あなたのHTTP responseをチェックすることです。コンピュータのブラウザからログインし、あなたの応答(Java/Androidコードでは、値を取得するためにString value = cookie.get(0).getValue();を使用しています)の正しい値を確認してください。

これは、あなたのドメインのクッキーを設定する方法である:

// Grab the domain of your cookie. 
String cookieOneDomain = cookie.get(0).getDomain(); 

CookieSyncManager.createInstance(this); 
CookieManager cookieManager = CookieManager.getInstance(); 
cookieManager.setAcceptCookie(true); 

cookieManager.setCookie(cookieOneDomain, cookieOne); 
関連する問題