2010-06-09 21 views
1

我有一個表單的提交按鈕onClickListener下面的代碼:顯示的HttpResponse在新的WebView(字符串從處理器)

String action, user, pwd, user_field, pwd_field; 

     action = "theURL"; 

     user_field = "id"; 
     pwd_field = "pw"; 
     user = "username"; 
     pwd = "password!!"; 

     List<NameValuePair> myList = new ArrayList<NameValuePair>(); 
     myList.add(new BasicNameValuePair(user_field, user)); 
     myList.add(new BasicNameValuePair(pwd_field, pwd)); 

     HttpParams params = new BasicHttpParams(); 
     HttpClient client = new DefaultHttpClient(params); 
     HttpPost post = new HttpPost(action); 
     HttpResponse end = null; 
     String endResult = null; 

     try { 
      post.setEntity(new UrlEncodedFormEntity(myList)); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      HttpResponse response = client.execute(post); 
      end = response; 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     BasicResponseHandler myHandler = new BasicResponseHandler(); 

     try { 
      endResult = myHandler.handleResponse(end); 
     } catch (HttpResponseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

我怎樣才能把生成的字符串(endResult)和使用意圖開始一個新的活動那將打開webview並加載html?

回答

4

你可以與

Intent myWebViewIntent = new Intent(context, MyWebViewActivity.class); 
myWebViewIntent.putExtra('htmlString', endResult); 
context.startActivity(myWebViewIntent); 

然後一個新的意圖,你MyWebViewActivity類,你會碰到這樣的:將上述答案如下之後

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.my_view_that_contains_a_webview); 
    WebView webview = (WebView)findViewById(R.id.my_webview); 

    Bundle extras = getIntent().getExtras(); 
    if(extras != null) { 

     // Get endResult 
     String htmlString = extras.getString('htmlString', ''); 
     webview.loadData(htmlString, "text/html", "utf-8"); 

    } 
} 
+0

這正是我期待的,謝謝! 問題:上下文指的是什麼? (我是新來的) 新課程需要延續什麼類型的活動? 不知道它是否重要,但它不會允許單引號htmlstring 最後,extras.getstring不會接受2個字符串參數,只有一個? – datguywhowanders 2010-06-10 15:51:29

+0

回答我自己的問題: 上下文需要設置爲NameOfMainAppClass.this。 第二類(帶有webview的類)只擴展了Activity。 單/雙引號無關緊要,並且將第二個參數從getString()中去除不會影響結果。 儘管如我在另一個StackOverflow文章中發現的那樣,loadData()不能很好地適用於html,並且使用完整方法loadDataWithBaseURL()獲得了更好的結果。 – datguywhowanders 2010-06-10 20:18:40

+0

酷,很高興你把它分類! – m6tt 2010-06-11 10:54:03

0

生成的代碼:

Intent myWebViewIntent = new Intent(YourAppClassHere.this, YourWebViewClassHere.class); 
myWebViewIntent.putExtra("htmlString", theStringThatHoldsTheHTML); 
startActivity(myWebViewIntent); 

我使用的基本webview類的完整代碼是:

public class MyWebView extends android.app.Activity{ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.web); 

    WebView webview = (WebView)findViewById(R.id.mainwebview); 

    Bundle extras = getIntent().getExtras(); 
    if(extras != null) { 

     // Get endResult 
     String htmlString = extras.getString("htmlString"); 
     webview.loadDataWithBaseURL(null, htmlString, "text/html", "utf-8", null); 
    } 

    } 
} 

這也是值得注意的是,這一點,我反正每次墜毀節目,直到我加入以下行的AndroidManifest.xml:

<activity android:name=".MyWebView"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

希望這會幫助別人了在未來:)感謝distrtrpect。

+0

它做了... 2年後:) Thx傢伙!一直在尋找這個解決方案4周,也許更多.. – 2012-03-19 07:50:12

0

如果你把你的偉大的方法,你得到的東西更好,我的代碼就只需要一個視圖,並用餅乾和POST變量的工作原理:d

private static final int TIMEOUT_MS = 3000; 
    private WebView mWebView; 
    private static final String redirURL = "http://www.somelogin.com/havefun.php"; 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 

     //------------------ COOKIES -----------------------// 
     CookieSyncManager.createInstance(this); 
     CookieManager cookieManager = CookieManager.getInstance(); 
     Date dateObj = new Date(); 

     dateObj.setTime(dateObj.getTime() + 2 * 7 * 24 * 60 * 60 * 1000); 
     String sA = "acc=" + 0; 
     String sL = "lgn="; 
     SimpleDateFormat postFormater = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz"); 
     String oD = postFormater.format(dateObj); 
     String cookieString = "logondata=" + sA + "&" + sL + "; expires="+ oD; 
     cookieManager.setCookie(redirURL, cookieString); 
     CookieSyncManager.getInstance().sync(); 



     //------------------ WEBVIEW -----------------------// 
     mWebView = (WebView) findViewById(R.id.webview); 

     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setSavePassword(true); 
     webSettings.setSaveFormData(true); 
     webSettings.setJavaScriptEnabled(true); 
     webSettings.setSupportZoom(false); 

     mWebView.setWebViewClient(new WebViewClient() { 
      public boolean shouldOverrideUrlLoading(WebView view, String url){ 
       // do your handling codes here, which url is the requested url 
       // probably you need to open that url rather than redirect: 
       view.loadUrl(url); 
       return false; // then it is not handled by default action 
      } 

     }); 

     //------------------------------ HTTP 4.0 REDIRECT --------------------------// 

     HttpClient httpClient = new DefaultHttpClient(); 
     HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS); 
     HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS); 
     HttpPost httpPost = new HttpPost(redirURL); 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("curl", "varl")); 
     nameValuePairs.add(new BasicNameValuePair("flags", "0")); 
     nameValuePairs.add(new BasicNameValuePair("forcedownlevel", "0"));  
     nameValuePairs.add(new BasicNameValuePair("formdir", "9")); 
     nameValuePairs.add(new BasicNameValuePair("username", "Tijs")); 
     nameValuePairs.add(new BasicNameValuePair("password", "mwhahah")); 
     nameValuePairs.add(new BasicNameValuePair("trusted", "1")); 
     HttpResponse end = null; 
     String endResult = null; 

     try { 
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpClient.execute(httpPost); 
      end = response; 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     BasicResponseHandler myHandler = new BasicResponseHandler(); 

     try { 
      endResult = myHandler.handleResponse(end); 
     } catch (HttpResponseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     mWebView.loadData(endResult, "text/html", "utf-8"); 

希望你喜歡這個代碼:P