安卓如何向指定URL发送get和post请求(傻瓜教程)
在以往的html前端开发时我们经常使用get和post请求向服务器请求数据,所以在安卓开发时如果我们能够像自己或第三方服务器请求数据那么就能够简化我们的开发,并实现更多的功能
接下来我们就用实际的例子讲解如何向指定的URL发送get请求
首先我们创建一个安卓项目并在主界面里创建一个按钮用来触发get请求,布局代码如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.john.getrequesttest.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送get请求"
android:id="@+id/sendGetRequest"/>
</RelativeLayout>
接下来在MainActivity里为button按钮添加监听事件,代码如下
package com.example.john.getrequesttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.sendGetRequest);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendGetRequest();//发送get请求
}
});
}
}
接下来进入我们的核心也就是发送get请求
- 首先我们要下载HttpClient的相关文件并导入项目中,下载地址如下
下载完成后解压放入libs文件夹中,注意把左上角切换为Project
选择相应的jar包右键选择Add As library…导入相关文件
在项目中引入这些包,相关代码如下
import org.apache.http.HttpEntity;
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 org.apache.http.util.EntityUtils;
- 然后我们要新建一个线程,注意一定要新建一个线程,因为安卓不允许在UI线程中发送网络请求,新建线程的代码如下
private void sendGetRequest(){//刚刚定义的方法
new Thread(new Runnable() {
@Override
public void run() {
//这里面书写新线程运行的代码
}
}).start();
}
- 创建HttpClient对象,并指定对象的URL参数,例如我们要想福建师范大学软件学院的官网请求数据,代码如下
HttpClient httpCient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://cse.fjnu.edu.cn/");
- 发送请求,代码如下
HttpResponse httpResponse = httpCient.execute(httpGet);
- 将获得的回复处理并转换成字符串打印在控制台
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity,"utf-8");
System.out.print(response);
- 最后不要忘记给程序添加网络权限,在AndroidManifest.xml添加网络权限,注意要加载application标签外面,代码如下
<uses-permission android:name="android.permission.INTERNET"/>
- 运行程序发现出现如下报错
- 这是由于jar包里出现了重复的文件,我们只需要忽略这些重复的文件就行了,在app build.gradle中加入如下代码就可以了
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
- 运行程序
- 控制台输出了软件学院官网的html代码,get请求获取成功了
- 这只是个简单的DEMO事实上我们要对收到的回复进行进一步处理,其实response可以接受和处理多种数据格式,例如json等,接下来粘出MainActivity所有的代码供大家参考
package com.example.john.getrequesttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.apache.http.HttpEntity;
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 org.apache.http.util.EntityUtils;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.sendGetRequest);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendGetRequest();//发送get请求
}
});
}
private void sendGetRequest(){//刚刚定义的方法
new Thread(new Runnable() {
public void run() {
//这里面书写新线程运行的代码
HttpClient httpCient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://cse.fjnu.edu.cn/");
try {
HttpResponse httpResponse = httpCient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity,"utf-8");
System.out.print(response);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
本人一直从事前端开发,刚接触安卓不久,在期末大作业项目中涉及到服务器和安卓交互的需求,故写了一些个人的研究,如有什么问题欢迎指正和交流