Home > Back-end >  Java development sina weibo fans assistant (mass by message)
Java development sina weibo fans assistant (mass by message)

Time:10-06


//get the subscriber list
Public final static String GETUID="HTTP://https://m.api.weibo.com/2/messages/subscribers/get.json? Access_token=access_token ";
//mass message interface
Public final static String SENDMSG="HTTP://https://m.api.weibo.com/2/messages/sendall.json? Access_token=access_token ";


//access_token effective length 1 year
Public final static String access_token="here for good";


Public static String post (JSONObject json, String URL) {
HttpClient client=new DefaultHttpClient ();
HttpPost post=new HttpPost (URL);
Post. SetHeader (" the content-type ", "application/x - WWW - form - urlencoded");
//post. AddHeader (" Authorization ", "Basic YWRtaW46");
The String result="";
Try {
StringEntity s=new StringEntity (json. The toString (), "utf-8");
S.s etContentEncoding (new BasicHeader (HTTP CONTENT_TYPE,
"X - WWW - form - urlencoded"));
Post. SetEntity (s);

//send the request
HttpResponse HttpResponse=client. The execute (post);

//get a response input stream
InputStream inStream=httpResponse. GetEntity (). GetContent ();
BufferedReader reader=new BufferedReader (new InputStreamReader (
InStream, "utf-8"));
The StringBuilder strber=new StringBuilder ();
The String line=null;
while ((line=reader.readLine()) !=null)
Strber. Append (line + "\ n");
InStream. Close ();
Result=strber. ToString ();
System. The out. Println (result);
If (httpResponse. GetStatusLine (.) getStatusCode ()==HttpStatus. SC_OK) {
System. The out. Println (" request to the server success, do the corresponding processing ");
} else {
System. The out. Println (" request to the server failure ");
}
} the catch (Exception e) {
Abnormal System. Out. Println (" request ");
Throw new RuntimeException (e);
}

Return the result.
}


Public static JSONArray getuid () {
String requestUrl=GETUID. Replace (" ACCESS_TOKEN ", ACCESS_TOKEN);
JSONObject json=WeixinUtil. HttpRequest (requestUrl, "GET", null);
JSONArray JSONArray=JSONArray. FromObject (JSONObject. FromObject (json. Get string (" data ")). Get string (" uids "));
Return jsonArray.
}



//this is call mass interface method

Public static String sendMsg (JSONObject outputStr) {
String requestUrl=SENDMSG. Replace (" ACCESS_TOKEN ", ACCESS_TOKEN);
String sb.=SinaUtil post (outputStr, requestUrl);
Return sb.
}





Here is the json data
{"touser":["1067095364","1659162414","1743030982","1770812821"],"articles":[{"summary":"测试群发","image":"http://images.csdn.net/20170601/timg-2_副本.jpg","display_name":"测试群发","url":"http://www.whjjyun.gov.cn/Frontpage/details-cultureinfo.html?id=592fd1f79fd1342c8ad12d6f&type=information"},{"summary":"活动","image":"http://jcjjwhy.oss-cn-hangzhou.aliyuncs.com/a53f6807671430ffeb4cd152cb7b9f8a.png","display_name":"测试测试测试","url":"https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn"}],"msgtype":"articles"}






I understand is using POST JSON data call this interface


{" error ":" Param error, see doc for more info ", "error_code:" 10008, "request" : "./2/messages/sendall json "}

Tried many times, can give me an error parameter errors, wondering why is this?
This interface should zha use? Great god answers for all of you!






CodePudding user response:

I also,,
  • Related