使用JSON请求调用REST服务。 这表示Http 415中的“不支持的介质类型”错误。
请求内容类型为“内容类型”、“应用程序/JSON; charset=utf8”)。
那个正常发挥着作用。 如果我要求的是不包括Json对象。 我使用谷歌- gson-2.2.4库json。
我试着用了几个不同的库,没有区别。
谁都能帮我解决这个问题吗?
这里是我的代码:
publicstaticvoidmain (字符串[ ] args ) throws Exception
{
jsonobjectrequestjson=newjsonobject (;
String url='xxx ';
//method call for generating json
请求JSON=generate JSON (;
URLmyurl=newURL(URL;
httpurlconnectioncon=(httpurl connection ) myurl.openConnection );
con.setdooutput(true;
con.setdoinput(true;
con.setrequestproperty (' content-type ',' application/json; charset=utf8 ';
con.setrequestproperty(accept,)应用程序/JSON );
con.setrequestproperty('method ',' POST );
output stream OS=con.get output stream (;
操作系统. write (请求JSON.tostring ).GetBytes('utf-8 ' );
操作系统. close (;
stringbuilder sb=new stringbuilder (;
inthttpresult=con.getresponse code (;
if (http result==httpurl connection.http _ ok ) {
bufferedreaderbr=newbufferedreader (newinputstreamreader (con.get inputstream )、' utf-8 ' ) )
字符串行=null;
while((line=br.readline ) )!=空) {
sb.append(line(n );
}
br.close (;
system.out.println (' sb.tostring ) );
}else{
系统. out.println (con.getresponse code () );
系统. out.println (con.getresponse message () );
}
}
publicstaticjsonobjectgeneratejson (throwsmalformedurlexception )
{
String s='http://www.abc.com ';
s .替换所有((/)、() )/);
jonobjectreqparam=newjsonobject (;
reqparam.addproperty('type ',' arl ' );
reqparam.addproperty('action ',' remove ' );
reqparam.addproperty('domain ',' staging ' );
reqparam.addproperty('objects ',s );
返回req param;
}
}
requestJson.toString的值如下:
{“类型”: ARL、action:remove、domain:staging、objects:3358 www.ABC.com }