注意:网络请求----一定要注意权限*********
1、Activity代码
public class MainActivity extends AppCompatActivity {
private String urlGet = "http://218.244.149.129:9010/api/companylist.php?industryid=99";
private String urlPost = "http://218.244.149.129:9010/api/companylist.php";
private String jsonUrl = "http://mobile.ximalaya.com/m/category_tag_menu";
private String imageUrl = "https://www.baidu.com/img/bd_logo1.png";
private RequestQueue queue;
private StringRequest requestGet;
private StringRequest requestPost;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView);
//创建RequestQueue
queue = Volley.newRequestQueue(this);
}
/**
* StringRequest Get请求
*
* @param view
*/
public void btnStringRequestGet(View view) {
//Get请求
requestGet = new StringRequest(Request.Method.GET, urlGet, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.i("main", "get-->" + s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}
);
queue.add(requestGet);
}
/**
* StringRequest Post请求
*
* @param view
*/
public void btnStringRequestPost(View view) {
requestPost = new StringRequest(Request.Method.POST, urlPost, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.i("main", "post-->" + s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("industryid", "99");
return map;
}
};
queue.add(requestPost);
}
/**
* 进行JsonRequest请求
*
* @param view
*/
public void json(View view) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonUrl,
null,//购物软件一般需要传递商品的Json对象
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Log.i("main", "json--->" + jsonObject.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
queue.add(jsonObjectRequest);
}
/**
* 进行ImageRequest请求
*
* @param view
*/
public void image(View view) {
ImageRequest imageRequest = new ImageRequest(imageUrl, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
if (bitmap != null) {
iv.setImageBitmap(bitmap);
}
}
}, 256, 128, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
queue.add(imageRequest);
}
}
2、布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:padding="10dp"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="btnStringRequestGet" android:text="StringGet联网请求" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="btnStringRequestPost" android:text="StringPost联网请求" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="json" android:text="JsonRequest请求" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="image" android:text="ImageRequest请求" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
错误:
android Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated wi
原因:
没有数据(没有地址与主机结合)
还是网络权限的问题
<uses-permission android:name="android.permission.INTERNET" />