2015-12-03 65 views
13

有一個似乎很常見的問題,但我已經完成了我的研究,並沒有看到它在任何地方都被完全重新創建。當我打印json.loads(rety.text)時,我看到了我需要的輸出。然而,當我打電話回來時,它向我顯示了這個錯誤。有任何想法嗎?非常感謝幫助,謝謝。我正在使用Flask MethodHandlerPython燒瓶,TypeError:'字典'對象不可調用

class MHandler(MethodView): 
    def get(self): 
     handle = '' 
     tweetnum = 100 

     consumer_token = '' 
     consumer_secret = '' 
     access_token = '-' 
     access_secret = '' 

     auth = tweepy.OAuthHandler(consumer_token,consumer_secret) 
     auth.set_access_token(access_token,access_secret) 

     api = tweepy.API(auth) 

     statuses = api.user_timeline(screen_name=handle, 
          count= tweetnum, 
          include_rts=False) 

     pi_content_items_array = map(convert_status_to_pi_content_item, statuses) 
     pi_content_items = { 'contentItems' : pi_content_items_array } 

     saveFile = open("static/public/text/en.txt",'a') 
     for s in pi_content_items_array: 
      stat = s['content'].encode('utf-8') 
      print stat 

      trat = ''.join(i for i in stat if ord(i)<128) 
      print trat 
      saveFile.write(trat.encode('utf-8')+'\n'+'\n') 

     try: 
      contentFile = open("static/public/text/en.txt", "r") 
      fr = contentFile.read() 
     except Exception as e: 
      print "ERROR: couldn't read text file: %s" % e 
     finally: 
      contentFile.close() 
     return lookup.get_template("newin.html").render(content=fr) 

    def post(self): 
     try: 
      contentFile = open("static/public/text/en.txt", "r") 
      fd = contentFile.read() 
     except Exception as e: 
      print "ERROR: couldn't read text file: %s" % e 
     finally: 
       contentFile.close() 
     rety = requests.post('https://gateway.watsonplatform.net/personality-insights/api/v2/profile', 
       auth=('---', ''), 
       headers = {"content-type": "text/plain"}, 
       data=fd 
      ) 

     print json.loads(rety.text) 
     return json.loads(rety.text) 


    user_view = MHandler.as_view('user_api') 
    app.add_url_rule('/results2', view_func=user_view, methods=['GET',]) 
    app.add_url_rule('/results2', view_func=user_view, methods=['POST',]) 

這裏是回溯(記住結果上面印刷):

Traceback (most recent call last): 
    File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__ 
    return self.wsgi_app(environ, start_response) 
    File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app 
    response = self.make_response(self.handle_exception(e)) 
    File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception 
    reraise(exc_type, exc_value, tb) 
    File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app 
    response = self.full_dispatch_request() 
    File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request 
    response = self.make_response(rv) 
    File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1577, in make_response 
    rv = self.response_class.force_type(rv, request.environ) 
    File "/Users/RZB/anaconda/lib/python2.7/site-packages/werkzeug/wrappers.py", line 841, in force_type 
    response = BaseResponse(*_run_wsgi_app(response, environ)) 
    File "/Users/RZB/anaconda/lib/python2.7/site-packages/werkzeug/test.py", line 867, in run_wsgi_app 
    app_rv = app(environ, start_response) 

回答

28

Flask only expects views to return a response-like object.這意味着Response,字符串或描述體,代碼和標頭的元組。你正在返回一個字典,這不是其中的一個。由於您要返回JSON,因此請返回正文中帶有JSON字符串的響應,其內容類型爲application/json

return app.response_class(rety.content, content_type='application/json') 

在你的榜樣,你已經有一個JSON字符串,通過你的請求返回的內容。但是,如果你想要一個Python結構轉換成JSON響應,使用jsonify

data = {'name': 'davidism'} 
return jsonify(data) 

在幕後,瓶是一個WSGI應用程序,它預計將繞過可調用的對象,這就是爲什麼你得到這個具體的錯誤:一個字典不可調用,Flask不知道如何將其轉化爲某種東西。

+0

謝謝davidism,這似乎減輕了錯誤。但是,現在我發現了一個新的錯誤,我認識到它可能與原始問題無關。這裏的任何想法? {u'code':400,u'error':u'JSON輸入在第1行第2列'} 127.0.0.1 - - [02/Dec/2015 23:39:34]「POST/results2 HTTP/1.1「200 - – puhtiprince

+0

@ puhtiprince這就是你提出的請求中的json,這是說你沒有做對。你需要傳遞'json ='來發布,而不是'data ='。 – davidism

5

使用Flask.jsonify函數返回數據。

示例 - return jsonify(data)

相關問題