2013-11-25 83 views
-1

我在我的Android應用程序中實現了JSP頁面。我不知道如何在Android中使用JSP Url。我試着運行應用程序。但是頁面是空白的並不顯示Android佈局中的任何信息,也不會顯示任何信息。這是我的代碼。如何在Android中調用JSP頁面

public class JSP_Activity extends Activity 
{ 
    public static String strUrl=null; 
    String strText = null; 

    public void OnCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.jsp_page); 
     connectWithGet_JspPage(); 
    } 


    private void connectWithGet_JspPage() 
    { 
     class GetJspPage extends AsyncTask<String, Void, String> 
     { 
      @Override 
      protected String doInBackground(String... strUrls) 
      { 

       // TODO Auto-generated method stub 

       strUrl="http://test.window2india.com/mobile/home.jsp"; 
       Log.e("strUrl :=","" + strUrl); 
       String strOutPut = null; 

        strOutPut=getOutPutFromUrl(strUrl); 
        Log.e("strOutPut :="," "+strOutPut); 
        return strOutPut.toString(); 

      } 

      protected void onPostExecute(String output1) 
      { 
       //outputText.setText(output1); 
       Log.e("strOutPut :="," "+output1); 
      } 

     } 
     GetJspPage getJspPageAsyncTask = new GetJspPage(); 
     getJspPageAsyncTask.execute(); 
    } 



    private String getOutPutFromUrl(String url) 
     { 

     StringBuffer output = new StringBuffer(""); 
     try 
     { 
      InputStream stream = getHttpConnection(url); 
      BufferedReader buffer = new BufferedReader(new InputStreamReader(stream)); 
      String s = ""; 
      while ((s = buffer.readLine()) != null) 
       output.append(s); 
     } 
     catch (IOException e1) 
     { 
      e1.printStackTrace(); 
     } 
     return output.toString(); 

    } 



    private InputStream getHttpConnection(String urlString) 
      throws IOException 
     { 
     InputStream stream = null; 
     URL url = new URL(urlString); 
     URLConnection connection = url.openConnection(); 

     try 

     { 
      HttpURLConnection httpConnection = (HttpURLConnection) connection; 
      httpConnection.setRequestMethod("GET"); 
      httpConnection.connect(); 

      if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) 
      { 
       stream = httpConnection.getInputStream(); 
      } 
     } 

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



} 
+0

HTTP URL的支持技術不相關。 –

+1

使用[webview](http://developer.android.com/reference/android/webkit/WebView.html) – VenomVendor

+1

使用webview組件加載jsp頁面,如webview.loadurl(「url.jsp」); – prakash

回答

1

試試這個代碼,這將有助於用於顯示的內容由jsp.This代碼發送是正常的佈局有用不webview.You必須解析您的自定義佈局內容和顯示。

 new Thread(new Runnable() 
      { 
        public void run() 
        { 
      try 
      { 

      URL url = new URL("http://test.window2india.com/mobile/home.jsp"); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
      InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
      BufferedReader r = new BufferedReader(new InputStreamReader(in)); 
      String x = ""; 
      String total = ""; 
      int i=0; 
      ArrayList<String> content = new ArrayList(); 
      while((x = r.readLine()) != null) 
      { 
         content.add(x); 

      } 
      in.close(); 
      r.close(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 

     } 

    } 

      }).start(); 
+0

你應該解釋你的回答 – keyser

+0

@ᴋᴇʏsᴇʀ我們可以顯示jsp發送的內容。 – Ram

+2

不對我。你應該解釋你在這裏提供的答案,不僅僅是說「試試這個:」,後面跟着一堆代碼。它不需要長時間的解釋,可能是一兩行。 – keyser