【问题标题】:sending JSON object using POST Methods使用 POST 方法发送 JSON 对象
【发布时间】:2011-11-11 02:29:23
【问题描述】:
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent in=getIntent();

        Uri uri=in.getData();

            // l.setText(uri.toString());
             String p=uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
             CreateFolderActivity.m_provider.setOAuth10a(true);
             try {
                CreateFolderActivity.m_provider.retrieveAccessToken(p);
            } catch (OAuthMessageSignerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OAuthNotAuthorizedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OAuthExpectationFailedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OAuthCommunicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             URL url = null;
                try {
                    url = new URL("http://api.mendeley.com/oapi/library/folders?consumer_key=" + CreateFolderActivity.m_consumer_key);


                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                HttpURLConnection hc=null;
                try {
                    hc=(HttpURLConnection)url.openConnection();
                    try {CreateFolderActivity.m_consumer.sign(hc);

                        hc.setRequestMethod("POST");
                        hc.setDoInput(true);
                        hc.setDoOutput(true);
                        hc.setUseCaches(false); 

                        hc.setRequestProperty("Content-type","text/json; charset=utf-8"); 
                        OutputStreamWriter wr = new OutputStreamWriter(hc.getOutputStream());
                        wr.write("folder = {'name' : 'Test creation folder'}");

                        wr.flush();

                        // Get the response
                     /*   BufferedReader rd = new BufferedReader(new InputStreamReader(hc.getInputStream()));
                        String strResponse = null;
                        for (String strLine = ""; strLine != null; strLine = rd.readLine()) 
                            strResponse += strLine ;*/
                        Log.i("HelloWorld",hc.getResponseMessage()+"    "+hc.getResponseCode());
                    } catch (OAuthMessageSignerException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (OAuthExpectationFailedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


  }
  }`

嗨,我正在尝试使用 post 方法发送一个 json 对象,上面是代码,但我收到内部服务器错误 500。当你发送一些意外数据时,我读取了它的出现。实际上它是一个 OAuth 实现,我必须添加一个用户帐户中的文件夹。我成功检索了访问令牌。请提出代码中的问题

【问题讨论】:

    标签: java android twitter-oauth oauth-2.0


    【解决方案1】:
    • "folder = {'name' : 'Test creation folder'}" 是无效的 JSON。 JSON Strings 必须用双引号 (") 括起来。我想你的意思是:

      {
          "folder": {
              "name": "Test creation folder"
          }
      }
      
      1. Refer to the JSON specification
      2. Validate your JSON
      3. Pretty print your JSON
    • 正确的 JSON MIME 类型是 application/json

    • 不要手动构建 JSON。使用org.json 包。首先查看JSONObjectJSONArray

    例子:

    hc.setRequestProperty("content-type","application/json; charset=utf-8"); 
    OutputStreamWriter wr = new OutputStreamWriter(hc.getOutputStream());
    JSONObject data = new JSONObject().put("folder",
                      new JSONObject().put("name", "test creation folder"));
    wr.write(data.toString());
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-07
    • 2022-06-14
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2014-03-17
    相关资源
    最近更新 更多