2012-02-21 23 views
7

में वेबव्यू में HTTP पोस्ट प्रतिक्रिया मैं HTTP पोस्ट का उपयोग कर पृष्ठ से कनेक्ट करने का प्रयास कर रहा हूं। मैं एक वेबव्यू बनाने के लिए एक http पोस्ट करता हूं। मुझे वेबव्यू से दूसरे पेज पर रीडायरेक्ट करने की आवश्यकता है। लेकिन जब जारी बटन क्लिक किया जाता है अपवाद फेंक दिया जाता है।एंड्रॉइड

मेरे कोड

public class ZHttpPostProjActivity extends Activity { 
    /** Called when the activity is first created. */ 

    private WebView mWebView; 
    private ProgressDialog progressBar; 
    private static final String TAG = "ZHttpPostProjActivity"; 

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

    mWebView = (WebView) findViewById(R.id.webview); 
    WebSettings webSettings = mWebView.getSettings(); 

    webSettings.setJavaScriptEnabled(true); 
    webSettings.setDomStorageEnabled(true); 

    progressBar = ProgressDialog.show(ZHttpPostProjActivity.this, "", 
     "Loading..."); 

    postData(); 
    } 


    private final String URL_REGISTER = "https://www.paypal.com/checkout"; 

    public void postData() { 

    BufferedReader bufferedReader = null; 

    try { 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("username", "username")); 
     nameValuePairs.add(new BasicNameValuePair("password", "password")); 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(URL_REGISTER); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, 
      HTTP.UTF_8)); 

     HttpResponse response = httpclient.execute(httppost); 

     bufferedReader = new BufferedReader(new InputStreamReader(response 
      .getEntity().getContent())); 
     StringBuffer stringBuffer = new StringBuffer(""); 
     String line = ""; 
     String LineSeparator = System.getProperty("line.separator"); 
     while ((line = bufferedReader.readLine()) != null) { 
     stringBuffer.append(line + LineSeparator); 
     } 
     bufferedReader.close(); 

     String webData = stringBuffer.toString(); 
     Log.i(TAG + "web data : ", webData); 

     // String webData = new 
     // BasicResponseHandler().handleResponse(response); 

     Log.i(TAG, "Httppost.getURI().toString(): " 
      + httppost.getURI().toString()); 

     mWebView.setWebViewClient(new WebViewClient() { 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      Log.i(TAG, "Processing webview url click..."); 
      view.loadUrl(url); 
      return true; 
     } 

     public void onPageFinished(WebView view, String url) { 

      Log.i(TAG, "Finished loading URL: " + url); 
      if (progressBar.isShowing()) { 
      progressBar.dismiss(); 
      } 
     } 

     public void onReceivedError(WebView view, int errorCode, 
      String description, String failingUrl) { 
      Log.e(TAG, "Error: " + description + " \n errorCode: " 
       + errorCode + "\n failingUrl: " + failingUrl); 

     } 
     }); 

     // mWebView.loadUrl(httppost.getURI().toString()); 
     mWebView.loadData(webData, "text/html", "UTF-8"); 
     mWebView.loadDataWithBaseURL(httppost.getURI().toString(), webData, 
      "text/html", HTTP.UTF_8, null); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

    } 

है पर क्लिक करने से एक ओर जहां वेबव्यू एक अपवाद

02-21 11:42:38.539: E/webviewdatabase(2848): Failed in setFormData 
02-21 11:42:38.539: E/webviewdatabase(2848): java.net.MalformedURLException: Unknown protocol: about 
02-21 11:42:38.539: E/webviewdatabase(2848): at java.net.URL.<init>(URL.java:288) 
02-21 11:42:38.539: E/webviewdatabase(2848): at java.net.URL.<init>(URL.java:157) 
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.WebViewDatabase.setFormData(WebViewDatabase.java:1032) 
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.BrowserFrame.loadStarted(BrowserFrame.java:384) 
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.JWebCoreJavaBridge.sharedTimerFired(Native Method) 
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.JWebCoreJavaBridge.fireSharedTimer(JWebCoreJavaBridge.java:91) 
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:108) 
02-21 11:42:38.539: E/webviewdatabase(2848): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-21 11:42:38.539: E/webviewdatabase(2848): at android.os.Looper.loop(Looper.java:123) 
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:673) 
02-21 11:42:38.539: E/webviewdatabase(2848): at java.lang.Thread.run(Thread.java:1019) 
02-21 11:42:41.324: E/cache(2848): illegal expires: Sat, Jan 01 2000 01:01:01 GMT 

उत्तर

7

उपयोग इस

WebView webview = new WebView(this); 
setContentView(webview); 
byte[] post = EncodingUtils.getBytes("postvariable=value&nextvar=value2", "BASE64"); 
webview.postUrl("http://www.geenie.nl/AnHeli/mobile/ranking/demo/index.php", post); 
+0

में विफल पोस्ट डेटा स्ट्रिंग उत्पन्न करते समय 'URLEncoder.encode (पासवर्ड," यूटीएफ -8 ") का उपयोग करने के लिए। –

1

फेंक दिया जाता है में बटन जारी रखने के लिए मुझे यकीन है कि नहीं कर रहा हूँ अगर यह वह जगह है जहाँ अपने असफल, या अगर यह कारण होगा एक त्रुटि, लेकिन चूंकि आप एक पोस्ट कर रहे हैं, तो "?" को हटा दें अपने URL_REGISTER चर के अंत में।

private final String URL_REGISTER = "https://www.paypal.com/checkout?"; 

"?" जीईटी अनुरोध करते समय केवल जरूरी है।

डेविड

+1

जैसे मैं हटा दिया "?" । एचटीपी पोस्ट को कॉल करने के बाद, पहला URL सफलतापूर्वक लोड हो गया, "https://www.paypal.com/checkout" पृष्ठ से https://www.paypal.com/conformation पर रीडायरेक्ट कर रहा है सेटफॉर्मडेटा अपवाद – jennifer