HttpTest

作者: 熊大哥87 | 来源:发表于2017-07-18 20:22 被阅读0次
    public class HttpUtil {
        public static void sendHttpRequest(final String adress,
                                             final HttpCallbackListenner listenner){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpURLConnection connection=null;
                    try{
                        URL url=new URL(adress);
                        connection=(HttpURLConnection)url.openConnection();
                        connection.setRequestMethod("GET");
                        connection.setReadTimeout(5000);
                        connection.setConnectTimeout(5000);
                        connection.setDoInput(true);
                        connection.setDoOutput(true);
                        InputStream in=connection.getInputStream();
                        BufferedReader reader=new BufferedReader(new InputStreamReader(in));
                        StringBuilder response=new StringBuilder();
                        String line;
                        while ((line=reader.readLine())!=null){
                            response.append(line);
                        }
                        if (listenner!=null){
                            listenner.onFinnsh(response.toString());
                        }
                    } catch (Exception e) {
                        if (listenner!=null){
                            listenner.onError(e);
                        }
                    } finally {
                        if (connection!=null){
                            connection.disconnect();
                        }
                    }
    
    
                }
            }).start();
        }
        public static void sendOkhttpRequest(String adress,okhttp3.Callback callback){
            OkHttpClient client=new OkHttpClient();
            Request request=new Request.Builder().url(adress).build();
            client.newCall(request).enqueue(callback);
        }
    }
    public interface HttpCallbackListenner {
        void onFinnsh(String response);
        void onError(Exception e);
    }
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private Button mBtnsendata1,mBtnsendata2;
        private TextView mTvshow;
        private Handler mainHandler= new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                 Bundle bundle=msg.getData();
                String data=bundle.getString("data");
                mTvshow.setText(data);
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mBtnsendata1= (Button) findViewById(R.id.senddata1);
            mBtnsendata2= (Button) findViewById(R.id.senddata2);
            mTvshow= (TextView) findViewById(R.id.show);
            mBtnsendata1.setOnClickListener(this);
            mBtnsendata2.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            String adrress="http://10.39.1.16";
            switch (view.getId()){
                case R.id.senddata1:
                    HttpUtil.sendHttpRequest(adrress, new HttpCallbackListenner() {
                        @Override public void onFinnsh(final String response) {
                           /* runOnUiThread(new Runnable() {
                                @Override
                                public void run() {*/
                                    mTvshow.setText(response);
    //                            }
    //                        });
    
                        }
    
                        @Override
                        public void onError(Exception e) {
    
                        }
                    });
                    break;
                case R.id.senddata2:
                    HttpUtil.sendOkhttpRequest(adrress, new okhttp3.Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
    
                        }
    
                        @Override
                        public void onResponse(Call call,  Response response) throws IOException {
                                    String resonsedata = response.body().string();
                                    Message message=Message.obtain();
                                    Bundle budle=new Bundle();
                                    budle.putString("data",resonsedata);
                                    message.setData(budle);
                                    mainHandler.sendMessage(message);
    
                        }
                    });
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:HttpTest

          本文链接:https://www.haomeiwen.com/subject/jcahkxtx.html