如何在Tomcat服务器上创建httppost

时间:2012-04-16 11:11:20

标签: android http-post

我正在开发一个应用程序,我希望在我的Tomcat服务器上执行HTTP post。在这篇文章中,我想发送用户ID和密码,并且在成功验证后,我希望下一页显示在我的模拟器中。我没有遇到与我的问题相关的堆栈溢出页面,但没有运气。我得到相同的登录页面。  我编写了如下代码。 感谢帮助。

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.food_service);
    mWebView = (WebView) findViewById(R.id.webview);
    postData();
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new HelloWebView());
}

public void postData() {
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username", "xxxx"));
        nameValuePairs.add(new BasicNameValuePair("password", "zzzzz"));
        String URL_STRING="http://190.168.11.2:8080/abcd/control/checkLogin/main";
        //(http://190.168.11.2:8080/abcd/control/checkLogin/main is url of login page)
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URL_STRING);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
        HttpResponse response = httpclient.execute(httppost);
        String data = new BasicResponseHandler().handleResponse(response);
        mWebView.loadDataWithBaseURL(httppost.getURI().toString()data,   "text/html",HTTP.PLAIN_TEXT_TYPE, null);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private class HelloWebView extends  WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        String  current_url=mWebView.getUrl();
        Log.d("url link",""+current_url);
        return true;
    }
}

0 个答案:

没有答案