2013-04-27 69 views
0

我想在Google App Engine上讀取JSON字符串(從我的Android應用獲取)。 但我不知道如何解決這個問題。GAE從Android App讀取JSON字符串

這是從我的應用程序的代碼:

JSONObject ob = new JSONObject(); 
    ob.put("user", "peter"); 
    ob.put("frage", "frage1"); 
    ob.put("datumende", "27.4.2012"); 
    Log.i("json", ob.toString()); 

    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", 
      "application/x-www-form-urlencoded"); 

    connection.setRequestProperty("Content-Language", "en-US"); 

    connection.setUseCaches(false); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 

    byte[] outputBytes = ob.toString().getBytes("UTF-8"); 
    connection.setRequestProperty("Content-Length", Integer.toString(outputBytes.length)); 
    OutputStream os = connection.getOutputStream(); 
    os.write(outputBytes); 

它的工作原理。如果我在數據存儲寫這些(getPostData)我可以

getPostData = self.request.body 

閱讀我的應用程序引擎這些數據拿出

{\"datumende\":\"27.4.2012\",\"user\":\"peter\",\"frage\":\"frage1\"} 

而且它的工作原理。

但是我怎樣才能將JSON字符串拆分爲用戶,datumende和frage? 我與

x = json.dumps(self.request.body) 
y = json.loads(x) 
# I thougt in y['user'] is now "peter". But it isnt't 

testet還我testet只

x = json.laods(self.request.body) 
# I thougt in x['user'] is now "peter". But it isnt't 

但這一切不工作。 那麼,如何將JSON字符串拆分爲用戶,frage,datumende?

回答

0

簡單的answere:/

客戶站點(安卓): 更換

connection.setRequestProperty("Content-Type", 
     "application/x-www-form-urlencoded"); 

connection.setRequestProperty("Content-Type", "application/json"); 

而這些在服務器站點(GAE與Python):

lo = self.request.body 
fff = json.loads(lo) 
name = fff['user']