Sending a JSON object using POST methods

@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(); } } }` 

Hi, I am trying to send a json object using the post method here, this is the code, but I get an internal server error 500.i read it when you send some unexpected data. In fact, this is an OAuth implementation, and I have to add the folder to the user account. And I retrieve the access token successfully. Please indicate what is wrong in the code

+4
java android twitter-oauth
source share
1 answer
  • "folder = {'name' : 'Test creation folder'}" invalid JSON. JSON Strings should be enclosed in double quotation marks ( " ). I think you meant this:

     { "folder": { "name": "Test creation folder" } } 
  • The correct Mime JSON type is application/json .

  • Do not create JSON manually. Use the org.json package. Start by looking for JSONObject and JSONArray .

Example:

 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()); 
+9
source share

All Articles