日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

HTTPUilConnection工具類

 印度阿三17 2019-04-14
package com.baway.firstweek.net;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.google.common.io.ByteStreams;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class AsyncHttpClient {
    private static final AsyncHttpClient ourInstance = new AsyncHttpClient();
    private static final String TAG = "AsyncHttpClient";

    public static AsyncHttpClient getInstance(){
        return ourInstance;
    }
    public void GetAsync(final String url,final AsyncCallback callback){
        new AsyncTask<String,Void,String>(){
            @Override
            protected String doInBackground(String... strings) {
                return GetDataHttp(strings[0]);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if (!TextUtils.isEmpty(s)){
                    callback.Succore(s);
                }else {
                    callback.Error(503,"未請(qǐng)求到數(shù)據(jù)");

                }
            }
        }.execute(url);
    }
    public void PostAsync(final String url, final String username, final String pwd, final AsyncCallback callback){
        new AsyncTask<String,Void,String>(){
            @Override
            protected String doInBackground(String... strings) {
                return PostDataHttp(strings[0], strings[1], strings[2]);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if (!TextUtils.isEmpty(s)){
                    callback.Succore(s);
                }else {
                    callback.Error(503, "未請(qǐng)求到數(shù)據(jù)");
                }
            }
        }.execute(url,username,pwd);
    }
    private String GetDataHttp(String server_url){
        try{
            URL url=new URL(server_url);
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == connection.HTTP_OK){
                InputStream stream=connection.getInputStream();
                String s=new String(ByteStreams.toByteArray(stream));
                return s;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    private String PostDataHttp(String server_url,String name,String pwd){
        try{
            URL url=new URL(server_url);
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            connection.setRequestMethod("POST");
            //請(qǐng)求頭的信息
            String body = "phone="   URLEncoder.encode(name)   "&pwd="   URLEncoder.encode(pwd);
            connection.getOutputStream().write(body.getBytes());
            if (connection.getResponseCode() == connection.HTTP_OK){
                InputStream stream=connection.getInputStream();
                String s = new String(ByteStreams.toByteArray(stream));
                return s;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public interface AsyncCallback{
        void Error(int errorcode,String message);

        void Succore(String result);
    }
}

?

?

來源:http://www./content-4-163901.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多