package csdn.com.login;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class Network2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_network2);
}
public void onClick_get(View view){
new Thread(networkTask_get).start();
}
public void onClick_post(View view){
new Thread(networkTask_post).start();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle data = msg.getData();
String val = data.getString("value");
Log.i("mylog=============", "请求结果为-->" + val);
// UI界面的更新等相关操作
}
};
Runnable networkTask_get = new Runnable() {
@Override
public void run() {
Message msg = new Message();
Bundle data = new Bundle();
data.putString("value", run_get());
msg.setData(data);
handler.sendMessage(msg);
}
};
Runnable networkTask_post = new Runnable() {
@Override
public void run() {
Message msg = new Message();
Bundle data = new Bundle();
data.putString("value", run_post());
msg.setData(data);
handler.sendMessage(msg);
}
};
public String run_get() {
HttpURLConnection connection = null;
try {
URL url = new URL("http://10.0.0.108/tp/public/index.php/index/index/t?month=12&day=1");
connection = (HttpURLConnection) url.openConnection();
// 设置请求方法,默认是GET
connection.setRequestMethod("GET");
// 设置字符集
connection.setRequestProperty("Charset", "UTF-8");
// 设置文件类型
connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
// 设置请求参数,可通过Servlet的getHeader()获取
connection.setRequestProperty("Cookie", "AppName=" + URLEncoder.encode("你好", "UTF-8"));
// 设置自定义参数
connection.setRequestProperty("MyProperty", "this is me!");
if (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
// result = StringStreamUtil.inputStreamToString(is);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
String result = sb.toString();
return result;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
public String run_post() {
HttpURLConnection connection = null;
try {
URL url = new URL("http://10.0.0.108/tp/public/index.php/index/index/t");
connection = (HttpURLConnection) url.openConnection();
// 设置请求方式
connection.setRequestMethod("POST");
// 设置编码格式
connection.setRequestProperty("Charset", "UTF-8");
// 传递自定义参数
connection.setRequestProperty("MyProperty", "this is me!");
// 设置容许输出
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
//将该字节数组作为请求体
byte[] requestBody = new String("name=旭&age=32").getBytes("UTF-8");
//将请求体写入到conn的输出流中
os.write(requestBody);
os.flush();
os.close();
// 获取返回数据
if(connection.getResponseCode() == 200){
InputStream is = connection.getInputStream();
// result = StringStreamUtil.inputStreamToString(is);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
String result = sb.toString();
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(connection!=null){
connection.disconnect();
}
}
return null;
};
}