2011-09-06 103 views
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對象發送JSON對象,但我得到的內部服務器錯誤500.i讀取它出現的時候你寄一些意想不到的data.Actually其一個OAuth實現,我必須在用戶添加一個文件夾account.and我檢索訪問令牌successfully.please所說的其實是錯誤的代碼使用POST方法

回答

9
  • "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()); 
+0

喜感謝現在的答覆其工作。 –

+0

@SahilMuthoo你可以請檢查我的問題[http://stackoverflow.com/questions/42024158/how-to-access-github-graphql-api-using-java] –