使用httpclient实现http接口调用实例
2015-02-09 23:56
651 查看
使用httpclient实现http接口调用实例
假设服务接口如下:
接口地址: http://192.168.0.1/service/sendsms
请求方式: post
需要传递参数: c= {"uid":"10000","title":"test a title","content":"this is a test"}
参数内容为json格式
输出:{result:0,code:"success"}
格式为json格式:result:1 .成功,0. 失败
code: 为提示信息
客户端调用代码:使用httpclient-4.0.1.jar
package com.yanek.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class TestSendSMS {
/**
* @param args
*/
public static void main(String[] args) {
String uid="12345678";
String title="test";
String content="test a content";
String ret=sendSms(uid ,title,content);
System.out.println(ret);
if(ret.indexOf("失败")<0)
{
System.out.println("成功发送sms");
}
else
{
System.out.println("失败发送");
}
}
public static String sendSms(String uid,String title,String content){
HttpClient httpclient = new DefaultHttpClient();
String smsUrl="http://192.168.0.1/service/sendsms";
HttpPost httppost = new HttpPost(smsUrl);
String strResult = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
JSONObject jobj = new JSONObject();
jobj.put("uid", uid);
jobj.put("title", title);
jobj.put("content",content);
nameValuePairs.add(new BasicNameValuePair("msg", getStringFromJson(jobj)));
httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
/*读返回数据*/
String conResult = EntityUtils.toString(response
.getEntity());
JSONObject sobj = new JSONObject();
sobj = sobj.fromObject(conResult);
String result = sobj.getString("result");
String code = sobj.getString("code");
if(result.equals("1")){
strResult += "发送成功";
}else{
strResult += "发送失败,"+code;
}
} else {
String err = response.getStatusLine().getStatusCode()+"";
strResult += "发送失败:"+err;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strResult;
}
private static String getStringFromJson(JSONObject adata) {
StringBuffer sb = new StringBuffer();
sb.append("{");
for(Object key:adata.keySet()){
sb.append("\""+key+"\":\""+adata.get(key)+"\",");
}
String rtn = sb.toString().substring(0, sb.toString().length()-1)+"}";
return rtn;
}
}
假设服务接口如下:
接口地址: http://192.168.0.1/service/sendsms
请求方式: post
需要传递参数: c= {"uid":"10000","title":"test a title","content":"this is a test"}
参数内容为json格式
输出:{result:0,code:"success"}
格式为json格式:result:1 .成功,0. 失败
code: 为提示信息
客户端调用代码:使用httpclient-4.0.1.jar
package com.yanek.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class TestSendSMS {
/**
* @param args
*/
public static void main(String[] args) {
String uid="12345678";
String title="test";
String content="test a content";
String ret=sendSms(uid ,title,content);
System.out.println(ret);
if(ret.indexOf("失败")<0)
{
System.out.println("成功发送sms");
}
else
{
System.out.println("失败发送");
}
}
public static String sendSms(String uid,String title,String content){
HttpClient httpclient = new DefaultHttpClient();
String smsUrl="http://192.168.0.1/service/sendsms";
HttpPost httppost = new HttpPost(smsUrl);
String strResult = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
JSONObject jobj = new JSONObject();
jobj.put("uid", uid);
jobj.put("title", title);
jobj.put("content",content);
nameValuePairs.add(new BasicNameValuePair("msg", getStringFromJson(jobj)));
httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
/*读返回数据*/
String conResult = EntityUtils.toString(response
.getEntity());
JSONObject sobj = new JSONObject();
sobj = sobj.fromObject(conResult);
String result = sobj.getString("result");
String code = sobj.getString("code");
if(result.equals("1")){
strResult += "发送成功";
}else{
strResult += "发送失败,"+code;
}
} else {
String err = response.getStatusLine().getStatusCode()+"";
strResult += "发送失败:"+err;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strResult;
}
private static String getStringFromJson(JSONObject adata) {
StringBuffer sb = new StringBuffer();
sb.append("{");
for(Object key:adata.keySet()){
sb.append("\""+key+"\":\""+adata.get(key)+"\",");
}
String rtn = sb.toString().substring(0, sb.toString().length()-1)+"}";
return rtn;
}
}
相关文章推荐
- 使用httpclient实现http接口调用实例
- 使用httpclient实现http接口调用实例
- 《转》使用httpclient实现http接口调用实例
- 使用httpclient实现http接口调用实例
- 使用httpclient实现http接口调用实例
- Android HTTP实例 使用GET方法和POST方法发送请求(通过Apache接口实现)
- Apache HttpClient 实现 Java 调用 Http 接口
- 使用httpclient实现图灵机器人web api调用实例
- 使用httpclient传递java参数调用http接口
- 使用HttpClient调用接口的实例讲解
- 使用httpclient传递java参数调用http接口
- 使用URL工具类调用webservice接口(soap)与http接口的实现方式
- 微信公众号 调用客服接口实现图文推送实例、
- Spring中HttpInvoker远程方法调用使用实例
- 使用httppost 实现高效的数据交互接口
- Android学习笔记47:使用HttpClient接口实现网络通信
- 服务器接口Android学习笔记47:使用HttpClient接口实现网络通信
- onvif规范的实现:使用gSOAP创建SOAP调用实例
- 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml
- js 调用后台方法 ICallbackEventHandler 接口实现局部刷新实例 ClientScript的GetCallbackEventReference方法实现局部刷新