休息服务没有给出预期的结果

时间:2012-07-25 03:57:05

标签: android rest salesforce apex-code

您好我在salesforce.com平台上的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作为request.bease的主体帮我如何获得正确的输出??

1 个答案:

答案 0 :(得分:0)

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

相关问题