httpclient(Get方法)
来源:互联网 发布:linux 查看caffe版本 编辑:程序博客网 时间:2024/06/08 07:06
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.example.httpclientrequestforget.utils.StreamTools;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//监听
public void btnHttpClientRequest(View view){
new Thread(){
@Override
public void run() {
super.run();
//调用方法
try {
httpClientRequestForGet();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 使用HttpClient这个api请求网络数据
* 请求方式:GET
* android 6.0以后,google不再推介使用这种方式,推介HttpURLConnection
*/
private void httpClientRequestForGet() throws Exception{
//1.打开浏览器
HttpResponse response = httpClient.execute(httpGet);
//4.等待服务器响应,获得响应状态码
//5.获取服务器响应的内容
InputStream is = response.getEntity().getContent();
String json = StreamTools.readFromNetWork(is);
System.out.println("httpclient : "+json);
}
import android.os.Bundle;
import android.view.View;
import com.example.httpclientrequestforget.utils.StreamTools;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//监听
public void btnHttpClientRequest(View view){
new Thread(){
@Override
public void run() {
super.run();
//调用方法
try {
httpClientRequestForGet();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 使用HttpClient这个api请求网络数据
* 请求方式:GET
* android 6.0以后,google不再推介使用这种方式,推介HttpURLConnection
*/
private void httpClientRequestForGet() throws Exception{
//1.打开浏览器
HttpClient httpClient = new DefaultHttpClient();
//2.填下地址HttpGet httpGet = new HttpGet("http://apis.juhe.cn/cook/query.php?menu=秘制红烧肉&key=ff00d7339861c7fd7d5b54b16b76422a");
HttpResponse response = httpClient.execute(httpGet);
//4.等待服务器响应,获得响应状态码
int code = response.getStatusLine().getStatusCode();
if(code == 200){//5.获取服务器响应的内容
InputStream is = response.getEntity().getContent();
String json = StreamTools.readFromNetWork(is);
System.out.println("httpclient : "+json);
}
}
需要build添加
android{
useLibrary 'org.apache.http.legacy'
}
阅读全文
0 0
- httpclient(Get方法)
- httpClient中的GET方法
- httpclient的get方法的官方API
- HttpClient的get方法获取网页内容
- HttpClient GET POST DELETE方法使用
- HttpClient的Post和Get方法
- HttpClient的get方法请求网络
- HttpClient应用案例post&get方法
- HttpClient(Post和Get)
- Android的httpclient的post方法获取服务器端的数据(服务器端是get方法)
- 使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据) (3)
- 通过Apache的HttpClient来实现HTTP的GET方法(自定义URI)
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)
- HttpClient之post方法和HttpUrlConnection之Get方法
- URLConnection的GET方法与POST方法,HttpClient的GET方法与POST方法
- httpclient客户端thread+handler+message(get)
- android:HttpClient请求(get、post)
- Android——HttpClient(get&post)
- 基于 REST 的 Web 服务:基础
- 邮件服务器&springboot---自动化的那些坑
- Android特效 五种Toast详解
- mysql存储过程语法详解
- Linux学习笔记四-Shell中[],(())
- httpclient(Get方法)
- label字体加粗
- Angular2图标选择器——jQuery asIconPicker
- 如何定义类总结
- [Motion]MPU9250的Motion Driver
- 长度为n的数组乱序存放着0至n-1. 现在只能进行0与其他数的交换.将数组中的元素按对应位置放置使得a[i]=i
- java日志框架学习
- Largest Submatrix of All 1’s POJ
- unity基本知识点2