2016-03-24 11 views
0

ウェブサービスコールでhtmlコンテンツを読み込みたいのですが、どのようにHTMLコンテンツをwebviewに読み込むことができますか?結果はSystem.out.println(data)に格納されます。 webviewでこのデータ値を読み取ることができません。コンストラクタも作成していますが、読み込めません。androidのhtmlコンテンツを読み込むには

public class Dashboard_Description__page extends AppCompatActivity { 
    ImageButton reader_back; 
    ArrayList<Reader_Model> actorsList; 
    String addCat; 
    ActorAdapter adapter; 


    WebView webView; 
    String alternate_id; 
    String bookmarkid; 
    String bookmarkfile; 
    private String webData; 
    String result; 

    String mimeType = "text/html"; 
    String encoding = "utf-8"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_dashboard__description__page); 

     webView = (WebView) findViewById(R.id.webview); 
     webView.loadDataWithBaseURL(null, result, "text/html", "UTF-8", null); 
     // String summary = "<html><body>You scored <b>192</b> points.</body></html>"; 
     //webView.loadData(getWebData(), "text/html", null); 
     WebViewClient yourWebClient = new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 


       return false; 
      } 

      @Override 
      public void onPageFinished(WebView view, String url) { 

       webView.loadUrl("javascript:window.HtmlViewer.showHTML" 
         + "(document.getElementsByTagName('pre')[0].innerHTML);"); 
      } 
     }; 
     // Get Web view 
     webView.getSettings().setJavaScriptEnabled(true); 
     // webView.addJavascriptInterface(new JSONReaderAsyncTask(), "HtmlViewer"); 
     webView.getSettings().setSupportZoom(true); 
     webView.getSettings().setBuiltInZoomControls(true); 
     webView.setWebViewClient(yourWebClient); 
     // Load URL 
     webView.loadUrl(url); 

     Dashboard_Description__page description__page=new Dashboard_Description__page(); 


     if (savedInstanceState == null) { 
      Bundle extras = getIntent().getExtras(); 

      if(extras == null) { 
       alternate_id= null; 
       bookmarkid= null; 
       bookmarkfile = null; 
      } else { 
       alternate_id= extras.getString("alternateid"); 
       bookmarkid= extras.getString("bookmarkid"); 
       bookmarkfile = extras.getString("bookmarkfile"); 

      } 
     } else { 
      alternate_id = (String) savedInstanceState.getSerializable("alternateid"); 
      bookmarkid= (String) savedInstanceState.getSerializable("bookmarkid"); 
      bookmarkfile= (String) savedInstanceState.getSerializable("bookmarkfile"); 

     } 

     // System.out.println(stringCameFromFirstAcvitity); 

     // actorsList = new ArrayList<Actors>(); 
     new JSONReaderAsyncTask().execute("https://www.miveive.com/mobile/get_article_detail?", " access_token", "bookmark_file", "alternate_id", "bookmarkId"); 


     reader_back=(ImageButton) 

       findViewById(R.id.reader_back_btn); 

     reader_back.setOnClickListener(new View.OnClickListener() 

     { 
      @Override 
      public void onClick (View v) { 
       Intent dash_back = new Intent(getApplicationContext(),Dashboard.class); 
       startActivity(dash_back); 

      } 
     }); 


    } 

    class JSONReaderAsyncTask extends AsyncTask<String, Void, Boolean> { 

     ProgressDialog dialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      dialog = new ProgressDialog(Dashboard_Description__page.this); 
      dialog.setMessage("Loading, please wait"); 
      dialog.setTitle("Connecting server"); 
      dialog.show(); 
      dialog.setCancelable(false); 


     } 

     @Override 
     protected Boolean doInBackground(String... params) 
     { 
      HttpParams httpParameters = new BasicHttpParams(); 

      HttpConnectionParams.setConnectionTimeout(httpParameters, 5000); 

      HttpConnectionParams.setSoTimeout(httpParameters, 5000); 
      HttpClient httpClient = new DefaultHttpClient(httpParameters); 

      HttpPost httpPost = new HttpPost(params[0]); 
      String jsonResult = ""; 
      try { 

       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4); 

       nameValuePairs.add(new BasicNameValuePair("access_token", "94529e5dbc6234fc3bbfce7406b8dde9")); 
       nameValuePairs.add(new BasicNameValuePair("bookmark_file", bookmarkfile)); 
       nameValuePairs.add(new BasicNameValuePair("alternate_id", alternate_id)); 
       nameValuePairs.add(new BasicNameValuePair("bookmarkId", bookmarkid)); 


       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpClient.execute(httpPost); 

       int status = 200; 

       if (status == 200) { 
        HttpEntity entity = response.getEntity(); 
        String data = EntityUtils.toString(entity); 
        System.out.println(data); 
        //System.out.println("fffff"); 
        JSONObject jsono = new JSONObject(data); 
        // JSONObject jsono1 = new JSONObject(data); 
        result = jsono.toString(); 
        System.out.println(result); 
        JSONReaderAsyncTask json=new JSONReaderAsyncTask(); 
        json.doInBackground(); 

//     JSONArray jarray = jsono.getJSONArray("images"); 
//     for (int i = 0; i < jarray.length(); i++) { 
//      JSONObject object = jarray.getJSONObject(i); 
//      System.out.println(object.getString("imageLink")); 
// 
//      Actors actor = new Actors(); 
//      // actor.getReaderImage(object.getString("content")); 
//      actor.getReaderImage(object.getString("imageLink")); 
// 
//      System.out.println(object.getString("imageId")); 
// 
//    //  actor.setDescription(object.getString("imageLink")); 
////      actor.setUrl(object.getString("bookmark_website")); 
//// 
////      actor.setBookmark_id(object.getString("bookmark_id")); 
////      actor.setAlternate_id(object.getString("alternate_id")); 
////      actor.setBookmark_file(object.getString("bookmark_file")); 
////      actor.setImage(object.getString("bookmark_preview_image")); 
// 
//      // actorsList.add(actor); 
//     } 
       } 
       return true; 


       //------------------>> 

      } catch (ParseException e1) { 
       e1.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return false; 
     } 


     protected void onPostExecute(Boolean result) { 
      dialog.cancel(); 
      // adapter.notifyDataSetChanged(); 
      if(result == false) 
       Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show(); 

     } 

    } 

} 
+1

htmlをロードすることができます。 –

+0

webviewを使ってhtmlファイルを読み込むことをお勧めします。 –

答えて

0

あなたは `WebViewの#loaddataの()`メソッドを使用します

webView.loadData(data, "text/html", "UTF-8"); 

チェックアウトの例として、以下のwebviewhere

+0

私はapiを打っていると私はwebviewでAPIの結果を表示したい –

関連する問題