使用POST方法发送JSON对象

4
  @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实现,并且我必须在用户帐户中添加一个文件夹。我已成功检索访问令牌,请建议代码有什么问题。

1个回答

9
  • "folder = {'name' : 'Test creation folder'}" is invalid JSON. JSON Strings must be enclosed with double-quotes ("). I think you meant this:

    {
        "folder": {
            "name": "Test creation folder"
        }
    }
    
    1. Refer to the JSON specification.
    2. Validate your JSON.
    3. Pretty print your JSON.
  • The correct JSON mime type is application/json.

  • Don't build your JSON by hand. Use the org.json package. Start by looking at JSONObject and JSONArray.

例子:

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());

@SahilMuthoo 你能否帮忙查看一下我的问题 [http://stackoverflow.com/questions/42024158/how-to-access-github-graphql-api-using-java]? - Kasun Siyambalapitiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接