2016-04-29 59 views
1

我想使用Airnotifier作爲GCM服務器。服務器現在放在我家裏。我將端口8000,8801,5228-5230轉發給服務器。我知道服務器正在工作,因爲我可以從另一個位置連接到Web界面。但是,現在的應用程序,我想註冊一個令牌,所以我用這個代碼:Airnotifier server 500 response JSON

String url = "http://URL:8801/api/v2/tokens"; 

    JSONObject json =new JSONObject(); 
    try { 
     json.put("device", "android"); 
     json.put("token","tokennnn"); 
     json.put("chanel","default"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    BufferedReader br = null; 
    HttpURLConnection connection = null; 
    try { 
     connection = (HttpURLConnection) new URL(url).openConnection(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    connection.setDoOutput(true); 
    connection.setInstanceFollowRedirects(false); 


    try { 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Content-Type", "application/json"); 
     connection.setRequestProperty("X-AN-APP-NAME", "appname"); 
     connection.setRequestProperty("X-AN-APP-KEY", "appkey"); 
     connection.setUseCaches(false); 
    } catch (ProtocolException e) { 
     e.printStackTrace(); 
    } 

    OutputStreamWriter wr= null; 
    try { 
     wr = new OutputStreamWriter(connection.getOutputStream()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    String jsonMessage = json.toString(); 
    try { 
     wr.write(jsonMessage); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    String httpresponsecode = null; 
    String httpresponsemessage = null; 

    httpresponsecode = Integer.toString(connection.getResponseCode()); 
    httpresponsemessage = connection.getResponseMessage(); 

    Log.i(TAG, "JSON Message: " + jsonMessage); 
    Log.i(TAG, "HTTP Response: " + httpresponsecode + ": " + httpresponsemessage); 

我嘗試過許多東西,但這是結果:

JSON Message: {"device":"android","token":"tokennnn","chanel":"default"} 
HTTP Response: 500: Internal Server Error 

和服務器我得到這樣的:

The problem on the server (IMAGE)

我知道有使用Python解碼JSON的一些問題。但我無法弄清楚這個問題。

編輯 找到了解決方案。在回答評論中看到答案

回答

0

我在絕望的最後一次嘗試中找到了答案。我一整天都在爲此工作。現在在問這裏之後,我找到了解決問題的辦法。需要關閉OutputStreamWriter .....

try { 
     wr.write(jsonMessage); 
     wr.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    }