2016-04-29 16 views
1

GCMサーバーとしてAirnotifierを使用します。サーバーは私の家に今置かれています。私は8000、8801、5228-5230のポートをサーバーに転送しました。別の場所からWebインターフェイスに接続できるので、サーバーが動作していることがわかります。しかし、今、私はので、私はこのコードを使用していたトークンを登録したいアプリで:Airnotifierサーバー500の応答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 

、サーバー上でIこれを取得:

The problem on the server (IMAGE)

私のpythonでデコードするいくつかのJSONの問題が存在しているはず。しかし、私は問題を理解することはできません。

編集 解決策が見つかりました。回答のコメントを参照してください

答えて

0

私は必死の最後の試みで答えを見つけました。私はこれを一日中働いていました。今ここで質問したところ、問題の解決策を見つけました。 OutputStreamWriterを閉じるために必要です.....

try { 
     wr.write(jsonMessage); 
     wr.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
関連する問題