如何从服务器

时间:2016-03-22 06:15:53

标签: java android

我想从服务器显示webview中的内容。数据来自服务器的html格式。如何在webview上这样。数据变量包含所有结果但我无法读取html内容。

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard__description__page);
        WebView webView = (WebView) findViewById(R.id.webview);
        // String summary = "<html><body>You scored <b>192</b> points.</body></html>";
        webView.loadData(summary, "text/html", null);


        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.webmod.com/mydata/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));

               // System.out.println(alternate_id);
                //System.out.println(bookmarkfile);
               // System.out.println(bookmarkid);
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpClient.execute(httpPost);
                // System.out.println("hello Hitu");

                //  jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
                // System.out.println(jsonResult);
                // StatusLine stat = response.getStatusLine();
                int status = 200;

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);
                  //  ArrayList<String> mylist = new ArrayList<String>();
                   // mylist.add(data);
                   // System.out.println(first);


                    System.out.println(data);
                    System.out.println("fffff");
                    JSONObject jsono = new JSONObject(data);
                    JSONArray jarray = jsono.getJSONArray("content");

                    }
                    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 个答案:

答案 0 :(得分:1)

奥基。正如您所说,您希望在WebView中显示这些数据,这就是您可以做的事情。

wv = (WebView) findViewById(R.id.webView)
wv.loadDataWithBaseURL("", YOUR_TEXT, "text/html", "utf-8", "");

我希望这会有所帮助。