使用httpclient实现http接口调用实例

使用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 nameValuePairs = new ArrayList();
				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;
	}
}


 

你可能感兴趣的:(Java技术,开源框架)