接口回调获取httpconnetion

*************************************************************************************************************************

Activity

package www.bawei.com.interface_huidiao;


import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import www.bawei.com.interface_huidiao.Bean.bean;
import www.bawei.com.interface_huidiao.JieKouGetJson.GetNet;

public class MainActivity extends AppCompatActivity implements GetNet.GetDate{

    private TextView textView;
private String json1=new String();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv);
        String url = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
       new Myasyn().execute(url);

    }

    @Override
    public void getDate(String json) {
        json1=json;
    }

    class Myasyn extends AsyncTask<String,Void,String>{



    @Override
    protected String doInBackground(String... strings) {
        GetNet getNet = new GetNet();
        getNet.OnGetDate(MainActivity.this);
        getNet.getJson(strings[0]);
        return json1;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Gson gson = new Gson();
        bean bean = gson.fromJson(s, bean.class);
        textView.setText(bean.getInfo());
    }
}
}

*************************************************************************************************

httpconnetion

package www.bawei.com.interface_huidiao.JieKouGetJson;

import android.util.Log;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by dell on 2017/12/27.
 */

public class GetNet {
    private GetDate getDate;

    public void getJson(String url) {
        try {
            URL url1 = new URL(url);
            URLConnection urlConnection = url1.openConnection();
            urlConnection.setReadTimeout(3000);
            urlConnection.setConnectTimeout(3000);
            InputStream inputStream = urlConnection.getInputStream();

            String s = I_InputStream(inputStream);

            getDate.getDate(s);
        } catch (MalformedURLException e) {
            e.printStackTrace();
//            getDate.getDate("诶丫丫,网络出问题了");
        } catch (IOException e) {
            e.printStackTrace();
//            getDate.getDate("诶丫丫,网络出问题了");
        }

    }

    public String I_InputStream(InputStream in) {

        StringBuffer sb = new StringBuffer();
        String len = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        try {
            while ((len = br.readLine()) != null) {
                sb.append(len);
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
//                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        byte[] bytes = new byte[1024];
//        int b=0;
//        try {
//            while (!(-1 == (b = in.read(b)))){
//                byteArrayOutputStream.write(bytes,0,b);
//                byteArrayOutputStream.flush();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        return sb.toString();
    }

    public interface GetDate {
        void getDate(String json);
    }

    public void OnGetDate(GetDate getDate) {
        this.getDate = getDate;
    }
}

*******************************************************************************************

Bean文件就不用了吧



猜你喜欢

转载自blog.csdn.net/sj_lpl_sb/article/details/78934668
今日推荐