0

嗨,我在 salesforce.com 平台上的 apex 中编写了一个基本的 Web 服务,这是 apex 代码

@RestResource(urlMapping='/gggg')

global with sharing class HelloWorld{

@HttpPost
global static String doPut(){
RestRequest req=RestContext.request;
RestResponse res=RestContext.response;
String body1=req.requestBody.toString();

  return body1;

}


}

我正在尝试使用 android 向该服务发送一个发布请求,首先我对其进行授权,然后获取访问令牌和刷新令牌,然后我再次获得 access_token,因此访问令牌没有问题。这里是 android 客户端的代码

  URL url1=new URL("https://ap1.salesforce.com/services/apexrest/gggg");
                                    HttpURLConnection connection=(HttpURLConnection) url1.openConnection();
                                    connection.addRequestProperty("Authorization", "OAuth "+str);
                                    connection.setRequestMethod("POST");
                                connection.setDoOutput(true);

                                String jsonbody="[{type:QwikScore_Question_Answer__c ,id:a03900000034MfA ,field:{id:Answer_Text__c,value:1.0}}]";
                                OutputStream st=connection.getOutputStream();
                                st.write(jsonbody.getBytes());
                                st.close();
                                    connection.connect();
                                    BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
                                    String ss="";
                                    String ss1="";
                                    while((ss=reader.readLine())!=null){
                                        ss1+=ss;
                                    }
                                    Log.i("Hello world",ss1);

在 Logcat for Hello World 的输出响应中,我得到“”作为输出,请指出为什么我没有得到正确的输出。我期待 [{type:QwikScore_Question_Answer__c,id:a03900000034MfA,field:{id:Answer_Text__c,value:1.0} }] 作为输出,因为我发送 st.write(jsonbody.getBytes()); jsonbody 作为请求的主体。请帮助我如何获得正确的输出?

4

1 回答 1

0

在您的请求中,您从未设置内容类型标头,因此服务器不知道如何解析有效负载。

于 2012-07-25T05:11:00.470 回答