android post get
16lz
2021-01-23
//post
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost("路径");
post.setHeader("Content-Type","application/x-www-form-urlencoded");
List<NameValuePair> pairs=new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("device_uuid",uuid));
pairs.add(new BasicNameValuePair("app_version"),app_version);
try{
post.setEntity(new UrlEncodedFormEntity(pairs));
//UrlEncodedFormEntity,这里第二个参数可以加"utf-8"
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
try{
client.execute(post);
}catch(Exeption e){
}
----------------------------------------------------------------------
//get
DefaultHttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(String url);//url参数,形如?x=1&y=2
HttpResponse response=client.execute(get);
int code=response.getStatusLine().getStatusCode();//返回码200 成功
if(code==200){
InputStream in=response.getEntity().getContent();//返回的数据
}
要添加权限: <uses-permission android:name="android.permission.INTERNET" />
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost("路径");
post.setHeader("Content-Type","application/x-www-form-urlencoded");
List<NameValuePair> pairs=new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("device_uuid",uuid));
pairs.add(new BasicNameValuePair("app_version"),app_version);
try{
post.setEntity(new UrlEncodedFormEntity(pairs));
//UrlEncodedFormEntity,这里第二个参数可以加"utf-8"
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
try{
client.execute(post);
}catch(Exeption e){
}
----------------------------------------------------------------------
//get
DefaultHttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(String url);//url参数,形如?x=1&y=2
HttpResponse response=client.execute(get);
int code=response.getStatusLine().getStatusCode();//返回码200 成功
if(code==200){
InputStream in=response.getEntity().getContent();//返回的数据
}
要添加权限: <uses-permission android:name="android.permission.INTERNET" />
更多相关文章
- Android 数据存储(数据库、文件、参数)操作实例
- Android中手机文件储存路径
- android 根据视频路径获取视频缩略图
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConn
- Android之SimpleAdapter简单实例和SimpleAdapter参数说明(zt)
- java/android 统计文件夹大小及删除文件夹下所有文件和路径
- 获得SDCard的路径
- Activity 属性设置参数说明