发送POST使用方法JSON对象 [英] sending JSON object using POST Methods

查看:224
本文介绍了发送POST使用方法JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        在意向= getIntent();        URI URI = in.getData();            // l.setText(uri.toString());
             串P = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
             CreateFolderActivity.m_provider.setOAuth10a(真);
             尝试{
                CreateFolderActivity.m_provider.retrieveAccessToken(P);
            }赶上(OAuthMessageSignerException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(OAuthNotAuthorizedException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(OAuthExpectationFailedException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(OAuthCommunicationException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }
             网址URL = NULL;
                尝试{
                    URL =新的URL(http://api.mendeley.com/oapi/library/folders?consumer_key=+ CreateFolderActivity.m_consumer_key);
                }赶上(MalformedURLException的E1){
                    // TODO自动生成catch块
                    e1.printStackTrace();
                }
                HttpURLConnection的HC = NULL;
                尝试{
                    HC =(HttpURLConnection类)url.openConnection();
                    尝试{CreateFolderActivity.m_consumer.sign(HC);                        hc.setRequestMethod(POST);
                        hc.setDoInput(真);
                        hc.setDoOutput(真);
                        hc.setUseCaches(假);                        hc.setRequestProperty(内容类型,文/ JSON;字符集= UTF-8);
                        OutputStreamWriter WR =新OutputStreamWriter(hc.getOutputStream());
                        wr.write(文件夹= {'名':'创建测试文件夹'});                        wr.flush();                        //获取响应
                     / *的BufferedReader RD =新的BufferedReader(新的InputStreamReader(hc.getInputStream()));
                        字符串strResponse = NULL;
                        对于(字符串strLine中=;!strLine中= NULL; strLine中= rd.readLine())
                            strResponse + = strLine中; * /
                        Log.i(HelloWorld的,hc.getResponseMessage()++ hc.getResponse code());
                    }赶上(OAuthMessageSignerException E){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }赶上(OAuthExpectationFailedException E){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
  }
  }`

你好我想在这里使用上面的帖子方法来发送一个JSON对象为code,但我得到的内部服务器错误500.i读取其出现时你寄一些意想不到的data.Actually它的一个OAuth实施与我有到一个文件夹中添加用户account.and我检索访问令牌successfully.please所说的其实是错误的code


解决方案

  • 文件夹= {'名':'创建测试文件夹'}是无效的JSON。

    :JSON 字符串必须用双引号()我觉得你的意思是这个括

      {
        文件夹:{
            名:创建测试文件夹
        }
    }


    1. 参考JSON规范

    2. 验证您的JSON

    3. pretty打印JSON


  • 正确的JSON MIME类型应用程序/ JSON


  • 不要建立由专人您的J​​SON。使用<$c$c>org.json包。通过查看的JSONObject JSONArray 开始。


例如:

  hc.setRequestProperty(内容类型,应用/ JSON的;字符集= UTF-8);
OutputStreamWriter WR =新OutputStreamWriter(hc.getOutputStream());
JSONObject的数据=新的JSONObject()。把(文件夹,
                  新的JSONObject()把(名,创建测试文件夹));
wr.write(data.toString());

  @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 post method here above is code but i am getting internal server error 500.i read its appear when u send some unexpected data.Actually its an OAuth implementation and i have to add a folder in the user account.and i retrieve access token successfully.please suggest what is wrong in code

解决方案

  • "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.

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

这篇关于发送POST使用方法JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆