发布数据HttpURLConnection给出500间隔服务器错误?

时间:2015-07-31 12:04:01

标签: android http httpurlconnection

我正在使用httpurlconnection进行连接网络服务。我尝试将json数据发送到body-> raw但是给我500个间隔错误。

StringBuffer chaine = new StringBuffer("");

URL url;
try {
      JSONObject jsonParam = new JSONObject();
      jsonParam.put("password", password);
      jsonParam.put("email", email);              
      url = new URL(urls[0]);
      HttpURLConnection connection = (HttpURLConnection)url.openConnection();
      connection.setRequestProperty("Content-Type", "application/json");
      connection.setRequestProperty("ApiKey", "ApiKey");
      connection.setRequestMethod("POST");
      connection.setDoInput(true);
      connection.setDoOutput(true);

      DataOutputStream printout = new DataOutputStream(connection.getOutputStream ());
      printout.writeBytes(jsonParam.toString());
      printout.flush ();
      printout.close ();

      connection.connect();
      Log.w("tag",connection.getResponseCode()+" "+connection.getResponseMessage());
      InputStream inputStream = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
      String line = "";
      while ((line = rd.readLine()) != null) {
                 chaine.append(line);
             }
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
}

     content = chaine.substring(0,chaine.length());

0 个答案:

没有答案