httpGET

来源:互联网 发布:js正则表达式匹配括号 编辑:程序博客网 时间:2024/06/05 15:52
package com.example.administrator.xfwb.util;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;/** * Created by Administrator on 2016-09-08. */public class Json {    public static String getJsonContent(String urlStr) {        try {// 获取HttpURLConnection连接对象            URL url = new URL(urlStr);            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();            // 设置连接属性            httpConn.setConnectTimeout(3000);            httpConn.setDoInput(true);            httpConn.setRequestMethod("GET");            // 获取相应码            int respCode = httpConn.getResponseCode();            if (respCode == 200) {                return ConvertStream2Json(httpConn.getInputStream());            }        } catch (MalformedURLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return "";    }    private static String ConvertStream2Json(InputStream inputStream)    {        String jsonStr = "";        // ByteArrayOutputStream相当于内存输出流        ByteArrayOutputStream out = new ByteArrayOutputStream();        byte[] buffer = new byte[1024];        int len = 0;        // 将输入流转移到内存输出流中        try        {            while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)            {                out.write(buffer, 0, len);            }            // 将内存流转换为字符串            jsonStr = new String(out.toByteArray());        }        catch (IOException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }        return jsonStr;    }}



package com.example.administrator.xfwb;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.KeyEvent;import android.view.Window;import android.view.WindowManager;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import com.example.administrator.xfwb.util.Json;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import java.text.SimpleDateFormat;public class H5Activity extends AppCompatActivity {    WebView mWebView;    private  WebSettings webSettings;    String surl;    String burl;    private final Handler handler = new Handler();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);//不显示程序的标题栏        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);//不显示系统的标题栏        setContentView(R.layout.activity_h2);        mWebView = (WebView) findViewById(R.id.webView);        webSettings = mWebView.getSettings();        mWebView.getSettings().setJavaScriptEnabled(true);        mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);        mWebView.getSettings().setLoadsImagesAutomatically(true);        mWebView.getSettings().setBuiltInZoomControls(true);        mWebView.getSettings().setSupportZoom(true);        mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);        mWebView.getSettings().setDefaultTextEncodingName("utf-8");        handler.postDelayed(task, 1000);    }    public boolean onKeyDown(int keyCode, KeyEvent event) {        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {            mWebView.goBack();            return true;        }        return super.onKeyDown(keyCode, event);    }    private final Runnable task = new Runnable() {        public void run() {            //if (auto) {  //自动模式                handler.postDelayed(this, 10000);                surl= Json.getJsonContent("http://192.168.1.214:858/88.html");                if (surl!=burl){                    burl=surl;                    mWebView.loadUrl(burl);                }            //}        }    };}


0 0
原创粉丝点击