package com.bw.httouriconnectiondemo;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import com.google.gson.Gson;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
/*getInfo("http://op.juhe.cn/onebox/basketball/nba");*/
postInfo("http://op.juhe.cn/onebox/basketball/nba","");
}
}).start();
}
private void getInfo(String path){
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(20000);
connection.setReadTimeout(20000);
connection.setDoOutput(true);
connection.setDoInput(true);
if (connection.getResponseCode() == 200){
InputStream is = connection.getInputStream();
String str = TreamToolsUtil.readInfo(is);
Gson gson = new Gson();
Nba.Result result = gson.fromJson(str, Nba.Result.class);
System.out.println(result);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void postInfo(String path,String string) {
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(20000);
connection.setReadTimeout(2000);
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStream os = connection.getOutputStream();
os.write(path.getBytes());
os.flush();
os.close();
while (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
String str = TreamToolsUtil.readInfo(is);
System.out.println(str);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
/*getInfo("http://op.juhe.cn/onebox/basketball/nba");*/
postInfo("http://op.juhe.cn/onebox/basketball/nba","");
}
}).start();
}
private void getInfo(String path){
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(20000);
connection.setReadTimeout(20000);
connection.setDoOutput(true);
connection.setDoInput(true);
if (connection.getResponseCode() == 200){
InputStream is = connection.getInputStream();
String str = TreamToolsUtil.readInfo(is);
Gson gson = new Gson();
Nba.Result result = gson.fromJson(str, Nba.Result.class);
System.out.println(result);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void postInfo(String path,String string) {
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(20000);
connection.setReadTimeout(2000);
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStream os = connection.getOutputStream();
os.write(path.getBytes());
os.flush();
os.close();
while (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
String str = TreamToolsUtil.readInfo(is);
System.out.println(str);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}