在第三方开店卖商品,我需要把我查到的数据用post形式发给第三方接口!!不会弄啊,
各位哥哥能帮帮我吗http://open.mengdian.com/doc/api/tag/zb_list.html
public static String postDataUTF8(Map<String,String> data, String url)
{
HttpClient httpClient = new HttpClient();
HttpConnectionManagerParams managerParams = httpClient
.getHttpConnectionManager().getParams();
// 设置连接超时时间(单位毫秒)
managerParams.setConnectionTimeout(connectionTimeout);
// 设置读数据超时时间(单位毫秒)
managerParams.setSoTimeout(soTimeout);
PostMethodUTF8 postMethod = new PostMethodUTF8(url);
Iterator<String> it = data.keySet().iterator();
NameValuePair[] sendData = new NameValuePair[data.keySet().size()];
int i = 0;
String key = "";
while(it.hasNext())
{
key = it.next();
sendData[i++] = new NameValuePair(key, data.get(key));
}
postMethod.setRequestBody(sendData);
postMethod.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
String strResponse = null;
try {
int statusCode = httpClient.executeMethod(postMethod);
//logger.info("--------Connect the interface at time:" + new Date());
if (statusCode != HttpStatus.SC_OK) {
strResponse = "error_第三方返回非200状态码";
throw new IllegalStateException("Method failed: "
+ postMethod.getStatusLine());
}
strResponse = postMethod.getResponseBodyAsString();
if(NetCheckFieldsUtil.isBlank(strResponse)){
strResponse = "error_第三方返回空数据";
}
//System.out.println("getResponseCharSet:"+postMethod.getResponseCharSet());
//System.out.println("getRequestCharSet:"+postMethod.getRequestCharSet());
}catch(UnsupportedEncodingException e){
e.printStackTrace();
logger.error("UnsupportedEncodingException:",e);
strResponse = "error_发生UnsupportedEncodingException异常,未能成功调用第三方";
}catch(IllegalStateException e){
e.printStackTrace();
logger.error("IllegalStateException:",e);
// strResponse = "error_发生IllegalStateException异常,未能成功调用第三方";
} catch (ConnectTimeoutException e) {
// System.out.println("time out");
e.printStackTrace();
// throw new java.net.SocketTimeoutException("time out");
logger.error("ConnectTimeoutException:",e);
strResponse = "error_请求第三方时发生ConnectTimeoutException异常";
} catch(SocketTimeoutException e){
e.printStackTrace();
logger.error("SocketTimeoutException:",e);
strResponse = "error_请求第三方时发生SocketTimeoutException异常";
}catch (Exception ex) {
ex.printStackTrace();
// throw new IllegalStateException(ex.toString());
logger.error("Exception:",ex);
strResponse = "error_请求第三方时发生未知异常";
} finally {
postMethod.releaseConnection();
}
//logger.info(strResponse);
return strResponse;
}
以这个接口为例:http://open.mengdian.com/doc/apiarticle/tag/Ib_doc
string json=" { "category_id": "538112937", "category_attr_name": "品牌", "category_attr_values": [ "苹果", "三星", "HTC" ] }";
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
//这里返回的结果就是实例返回的json字符串
} else {
throw new IOException("Unexpected code " + response);
}
}