Airnotifier服务器500响应JSON

时间:2016-04-29 13:58:03

标签: android python json

我想将Airnotifier用作GCM服务器。服务器现在放在我家。我将端口:8000,8801,5228-5230转发到服务器。我知道服务器正在运行,因为我可以从其他位置连接到Web界面。但现在在应用程序中我想注册一个令牌,所以我使用这个代码:

    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问题。但我无法弄清楚问题。

修改 找到了解决方案。请参阅答案评论中的答案

1 个答案:

答案 0 :(得分:0)

我在绝望的最后一次尝试中找到了答案。我整天都在研究这个问题。现在在问这里之后我找到了问题的解决方案。需要关闭OutputStreamWriter .....

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