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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 这几年快速阅读市场火爆,主张也很简单:牛人都看过几百上千本书,要变得和他们一样牛逼,你不快速阅读怎么能行!?于是,...