android http将响应发布到webview并导航

android http将响应发布到webview并导航,android,http-post,webviewclient,Android,Http Post,Webviewclient,我正在制作一个向Ilias服务器发出post请求的应用程序,我收到响应并在网络视图上打开它,我的问题是,如果我导航,当我单击某个链接时,它不会保存我的凭据并提示我“你未登录”,并且不会继续 代码如下: public class ilias extends Activity { WebView webView; /** Called when the activity is first created. */ @Override public void onCreat

我正在制作一个向Ilias服务器发出post请求的应用程序,我收到响应并在网络视图上打开它,我的问题是,如果我导航,当我单击某个链接时,它不会保存我的凭据并提示我“你未登录”,并且不会继续

代码如下:

public class ilias extends Activity {

 WebView webView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webView = (WebView)findViewById(R.id.webview);

        BufferedReader bufferedReader = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost request = new HttpPost("http://www.ilias.de/docu/login.php?client_id=docu");
        List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("username", "stacked")); //this username 
        postParameters.add(new BasicNameValuePair("password", "overflow"));//works


  try {
   UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
         request.setEntity(entity);

         HttpResponse response= httpClient.execute(request);

   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();

   Toast.makeText(ilias.this, 
     "Finished", 
     Toast.LENGTH_LONG).show();

   String webData = stringBuffer.toString();

   webView.loadData(webData,"text/html","UTF-8");
   webView.loadDataWithBaseURl("http://www.ilias.de/docu/",webData,"text/html","UTF-8","about:blank");

  } catch (ClientProtocolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(ilias.this, 
     e.toString(), 
     Toast.LENGTH_LONG).show();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(ilias.this, 
     e.toString(), 
     Toast.LENGTH_LONG).show();
  }finally{
   if (bufferedReader != null){
    try {
     bufferedReader.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

    }
}
公共课堂ilias扩展活动{
网络视图;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView=(webView)findviewbyd(R.id.webView);
BufferedReader BufferedReader=null;
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost请求=新建HttpPost(“http://www.ilias.de/docu/login.php?client_id=docu");
List postParameters=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,“堆叠”);//此用户名
add(新的BasicNameValuePair(“密码”,“溢出”);//有效
试一试{
UrlEncodedFormEntity实体=新的UrlEncodedFormEntity(后参数);
请求。设置实体(实体);
HttpResponse response=httpClient.execute(请求);
bufferedReader=新的bufferedReader(
新的InputStreamReader(response.getEntity().getContent());
StringBuffer StringBuffer=新的StringBuffer(“”);
字符串行=”;
字符串LineSeparator=System.getProperty(“line.separator”);
而((line=bufferedReader.readLine())!=null){
stringBuffer.append(行+行分隔符);
}
bufferedReader.close();
Toast.makeText(ilias.this,
“完成”,
Toast.LENGTH_LONG).show();
String webData=stringBuffer.toString();
loadData(webData,“text/html”,“UTF-8”);
webView.loadDataWithBaseURl(“http://www.ilias.de/docu/,webData,“文本/html”、“UTF-8”、“关于:空白”);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
Toast.makeText(ilias.this,
e、 toString(),
Toast.LENGTH_LONG).show();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
Toast.makeText(ilias.this,
e、 toString(),
Toast.LENGTH_LONG).show();
}最后{
if(bufferedReader!=null){
试一试{
bufferedReader.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
}

以这种方式发布帖子,您将失去存储cookie的能力。您还必须在
WebView
中模拟服务器的身份验证机制。您可以直接将来自WebView的帖子与请求正文一起使用。您似乎通过URL编码的实体传递用户名和密码,该实体与WebView的URL兼容

尝试以下方法:

String query = "username=" + username + "&password=" + password";
webview.postUrl("http://www.ilias.de/docu/login.php?client_id=docu", query.getBytes());

如果您多次使用httppost,则可以通过httpclient维护cookies。如果你只是在一个网站中导航,那么该网站实际上应该存储你的信息,因为它是由该网站处理的东西,否则,将httpclient设置为公共静态全局,并在整个活动中或通过其他活动共享它。

但我应该何时调用postrl方法?继续执行相同的操作。总是点击我得到的链接“你没有登录”。您可以测试代码、用户名和密码是否有效。当我试图在设备上打开链接时,我一直无法使用该网页,该网页在浏览器中工作