2015-09-20 22 views
0

我想將servlet的輸出寫入android活動。但在活動中,我只獲取html數據流。我如何在Android中顯示html網頁顯示?servlet將html表單寫入Android

NewServlet.java

@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"}) 
    public class NewServlet extends HttpServlet { 


    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     try (PrintWriter out = response.getWriter()) { 
      /* TODO output your page here. You may use following sample code. */ 
      out.println("<html>\n" + 
      " <body>\n" + 
      "  <input type=\"submit\" value=\"click me\" />\n" + 
      " </body>\n" + 
      "</html>"); 
     } 
    } 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 


    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 


    @Override 
    public String getServletInfo() { 
     return "Short description"; 
    }// </editor-fold> 

} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 


    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/textView" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="84dp" /> 
</RelativeLayout> 

MainActivity.java

package com.example.bright.myurldemo; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 

public class MainActivity extends Activity { 

    TextView textView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textView = (TextView)findViewById(R.id.textView); 
     BackProcess backProcess = new BackProcess(); 
     backProcess.execute(); 

    } 
    boolean flag = true; 
    String string; 
    class BackProcess extends AsyncTask{ 
     StringBuffer output = new StringBuffer(""); 
     @Override 
     protected String doInBackground(Object[] params) { 
      try{ 
       URL url = new URL("http://10.11.55.45:8080/WebApplication3/NewServlet"); 
       URLConnection urlConnection = url.openConnection(); 
       HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection; 
       int responseCode = httpURLConnection.getResponseCode(); 

       if(responseCode == HttpURLConnection.HTTP_OK){ 
        InputStream inputStream = httpURLConnection.getInputStream(); 
        BufferedReader buffer = new BufferedReader(
          new InputStreamReader(inputStream)); 
        String s = ""; 
        while ((s = buffer.readLine()) != null) 
         output.append(s); 
       } 
      }catch (Exception e){ 
       //Toast.makeText(getApplicationContext(), e.getMessage()+"", Toast.LENGTH_LONG).show(); 
       flag = false; 
       string = e.getMessage(); 
      } 
      return output.toString(); 
     } 

     @Override 
     protected void onPostExecute(Object o) { 
      if(flag) 
       textView.setText(output); 
      else 
       textView.setText(string); 
     } 
    } 

} 

回答

0

如果我理解正確的話,你要顯示的網頁本身,像一個瀏覽器。您無法使用文本視圖來完成此操作。您需要使用一個名爲WebView的組件。

這裏有一個簡單的例子:

<?xml version="1.0" encoding="utf-8"?> 
<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/webpage" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
/> 

WebView webView = (WebView) findViewById(R.id.webpage); 
webView.loadUrl("http://10.11.55.45:8080/WebApplication3/NewServlet"); 
+0

感謝üPaunov。它的工作正常。 –

+0

樂意幫忙:)如果解決了你的問題,你可以接受答案。 –