您的位置:首页 > 移动开发 > Android开发

Android直接POST发送字符串

2016-05-23 10:39 323 查看
String uriAPI = "http://192.168.11.108:2492/json/SendMessage/";
HttpPost httpRequest =new HttpPost(uriAPI);
try{
StringEntity myEntity = new StringEntity("abc");
httpRequest.setEntity(myEntity);
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//若状态码为200 ok
if(httpResponse.getStatusLine().getStatusCode()==200){
String strResult=EntityUtils.toString(httpResponse.getEntity());
}
else
{
Log.v("tag", "Error Response"+httpResponse.getStatusLine().toString() );
}
}catch(ClientProtocolException e){
e.printStackTrace();
} catch (UnsupportedEncodingException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}


主要实设置myEntity 的内容设置为要上传的string替换掉原来的Entity 即可。

StringEntity myEntity = new StringEntity("abc");
httpRequest.setEntity(myEntity);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: