Post与Get一样 都是先创建一个按钮
<Button
android:text="get请求"
android:onClick="HttpGet"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
<Button
android:text="post请求"
android:onClick="HttPost"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
提前要添加好网络权限
![](https://img-blog.csdnimg.cn/img_convert/743b85e82fec5ccc08900db91c49d3b6.png)
之后基本就是死代码了 靠手动记忆记住了
GET请求
public String getString(String path){
try {
URL url=new URL(path);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(2000);
httpURLConnection.setReadTimeout(2000);
httpURLConnection.connect();
if (httpURLConnection.getResponseCode()==200)
{
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String str="";
StringBuilder stringBuilder=new StringBuilder();
while ((str=bufferedReader.readLine())!=null)
{
stringBuilder.append(str);
}
String json=stringBuilder.toString();
Log.i("---json","getString"+json);
return json;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
之后再前端调用即可
public void HttpGet(View view) {
new MyAsyncTask().execute("http://43.143.146.165:7777/foods/getFoods?pageSize=10¤tPage=1");
}
public class MyAsyncTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
String path=strings[0];
String json=HttpManager.getInstance().getString(path);
return json;
}
}
Post的请求也相似
Set<Map.Entry<String,Integer>> entries=hashMap.entrySet();
for (Map.Entry<String,Integer> entry:entries)
{
String key=entry.getKey();
Integer value=entry.getValue();
if (isfirst)
{
isfirst=false;
}else
{
boby+="&";
}
boby+= URLEncoder.encode(key,"UTF-8")+"="+value;
}
Log.i("---boby","postString"+boby);
OutputStream outputStream=httpURLConnection.getOutputStream();
outputStream.write(boby.getBytes());
outputStream.flush();
if (httpURLConnection.getResponseCode()==200)
{
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String str="";
StringBuilder stringBuilder=new StringBuilder();
while ((str=bufferedReader.readLine())!=null)
{
stringBuilder.append(str);
}
String json=stringBuilder.toString();
Log.i("---json","postString"+json);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
之后进行前端调用
public void HttPost(View view) {
new MuAsycTaskPoat().execute("http://43.143.146.165:7777/foods/postFoods");
}
public class MuAsycTaskPoat extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String path=strings[0];
HashMap<String,Integer> stringIntegerHashMap=new HashMap<>();
stringIntegerHashMap.put("currentPage",1);
stringIntegerHashMap.put("PageSize",10);
HttpManager.getInstance().postString(path,stringIntegerHashMap);
return null;
}
}
主要就是多敲 形成肌肉记忆 就行了 多敲多练