Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用POST方法发送JSON对象_Java_Android_Twitter Oauth_Oauth 2.0 - Fatal编程技术网

Java 使用POST方法发送JSON对象

Java 使用POST方法发送JSON对象,java,android,twitter-oauth,oauth-2.0,Java,Android,Twitter Oauth,Oauth 2.0,您好,我正在尝试使用post方法发送json对象,上面是代码,但我收到内部服务器错误500。我在发送一些意外数据时读取了它的显示。实际上,这是OAuth实现,我必须在用户帐户中添加一个文件夹。我已成功检索访问令牌。请建议代码中的错误所在 “文件夹={'name':'testcreation folder'}”是无效的JSON。JSON字符串必须用双引号(“)括起来。我想你的意思是: @Override public void onCreate(Bundle savedInstance

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

  • “文件夹={'name':'testcreation folder'}”
    是无效的JSON。JSON
    字符串
    必须用双引号(
    )括起来。我想你的意思是:

      @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();
                    }
    
    
      }
      }`
    
  • 正确的JSON mime类型是
    application/JSON

  • 不要手工构建JSON。使用包。首先查看
    JSONObject
    JSONArray

例如:

{
    "folder": {
        "name": "Test creation folder"
    }
}

@SahilMuthoo你能检查一下我的问题吗[
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());