package com.example.unit2_getdata_post_demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import com.google.gson.Gson;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.Menu;
import android.view.View;
/**
* HttpURLConnection 执行post请求的案例
* @author e531
*
*/
public class MainActivity extends Activity {
private Handler myHandler=new Handler(){
public void handleMessage(android.os.Message msg) {
List<News> lists=(List<News>)msg.obj;
//设置适配器,显示
};
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void requestNews(View v){
new Thread(){
public void run() {
//注意:进行post请求时,请求的地址是不包含 参数信息的(?...)
String path="http://v.juhe.cn/toutiao/index";
try {
//1.创建一个URL
URL url=new URL(path);
//2.打开连接
HttpURLConnection openConnection =(HttpURLConnection) url.openConnection();
//3.设置一些设置
openConnection.setRequestMethod("POST");//post必须要大写
openConnection.setReadTimeout(3000);
openConnection.setConnectTimeout(3000);
//4.设置输出参数
openConnection.setDoOutput(true);
//请求的参数
String params="type=keji&key=c4479ad58f41e7f78a8fa073d0b1f1b5";
openConnection.getOutputStream().write(params.getBytes());
//5.得到响应码
int code=openConnection.getResponseCode();
if(code==200){
//6.得到结果
InputStream inputStream = openConnection.getInputStream();
//结果
String content=streamToString(inputStream);
Log.d("zzz", content);
//进行解析
Gson gs=new Gson();
Result result=gs.fromJson(content, Result.class);
Log.d("zzz", result.toString());
//新闻列表数据
List<News> datas = result.getResult().getData();
//发送消息
Message msg=Message.obtain();
msg.obj=datas;
myHandler.sendMessage(msg);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
/**
* 将字节流信息的内容读取出来
* @param is
* @return
*/
public String streamToString(InputStream is){
StringBuilder builder=new StringBuilder();
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
String con;
try {
while((con=reader.readLine())!=null){
builder.append(con);
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return builder.toString();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import com.google.gson.Gson;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.Menu;
import android.view.View;
/**
* HttpURLConnection 执行post请求的案例
* @author e531
*
*/
public class MainActivity extends Activity {
private Handler myHandler=new Handler(){
public void handleMessage(android.os.Message msg) {
List<News> lists=(List<News>)msg.obj;
//设置适配器,显示
};
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void requestNews(View v){
new Thread(){
public void run() {
//注意:进行post请求时,请求的地址是不包含 参数信息的(?...)
String path="http://v.juhe.cn/toutiao/index";
try {
//1.创建一个URL
URL url=new URL(path);
//2.打开连接
HttpURLConnection openConnection =(HttpURLConnection) url.openConnection();
//3.设置一些设置
openConnection.setRequestMethod("POST");//post必须要大写
openConnection.setReadTimeout(3000);
openConnection.setConnectTimeout(3000);
//4.设置输出参数
openConnection.setDoOutput(true);
//请求的参数
String params="type=keji&key=c4479ad58f41e7f78a8fa073d0b1f1b5";
openConnection.getOutputStream().write(params.getBytes());
//5.得到响应码
int code=openConnection.getResponseCode();
if(code==200){
//6.得到结果
InputStream inputStream = openConnection.getInputStream();
//结果
String content=streamToString(inputStream);
Log.d("zzz", content);
//进行解析
Gson gs=new Gson();
Result result=gs.fromJson(content, Result.class);
Log.d("zzz", result.toString());
//新闻列表数据
List<News> datas = result.getResult().getData();
//发送消息
Message msg=Message.obtain();
msg.obj=datas;
myHandler.sendMessage(msg);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
/**
* 将字节流信息的内容读取出来
* @param is
* @return
*/
public String streamToString(InputStream is){
StringBuilder builder=new StringBuilder();
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
String con;
try {
while((con=reader.readLine())!=null){
builder.append(con);
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return builder.toString();
}
}