用json字符串记录loggly日志问题

时间:2016-02-18 21:26:24

标签: loggly

我将以下字符串发送给Loggly:

  public String userID = "";
       public String auth = "";
[![enter image description here][1]][1]       public String token ="";
       public String printAcounts = "";
       public String accountType = "";
       public String accountID = "";
       public String accountPassword = "";
       public String streamingURL = "";
       public String accountUsername = "";

       class SendRequsts extends Thread{
           private Thread makeRequests;

           public void run (){
               try {
                   makePost("https://website.com/auth/token","{\"auth\":{\"user\":"
                           + "{\"id\":\"username\",\"password\": \"password\"},\"method\":\"user\"}}");
                   sendGet();
               } catch (IOException e) {
                   e.printStackTrace();
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
           public void start(){
               if ( makeRequests == null ){
                   makeRequests = new Thread(this);
                   makeRequests.start();
               }
           }
       }

       @TargetApi(Build.VERSION_CODES.KITKAT)
       public void makePost(String requestedURL,String params) throws IOException{

           runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   Toast.makeText(MainActivity.this,token,
                           Toast.LENGTH_LONG).show();
               }
           });
           byte[] postData = params.getBytes(StandardCharsets.UTF_8);
           int postDataLength = postData.length;
           URL url = new URL( requestedURL );
           HttpsURLConnection connection = ( HttpsURLConnection ) url.openConnection();
           connection.setDoOutput(true);
           connection.setInstanceFollowRedirects(false);
           connection.setRequestMethod("POST");
           connection.setRequestProperty("Content-Type", "application/json");
           connection.setRequestProperty("charset", "utf-8");
           connection.setRequestProperty("Content-Length", Integer.toString(postDataLength));
           connection.setUseCaches(false);

           try {

               DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
               outputStream.write(postData);
               int respCode = connection.getResponseCode();

               InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
               BufferedReader inputReader = new BufferedReader(inputStreamReader);
               String readLine;
               StringBuilder response = new StringBuilder();

               while ((readLine = inputReader.readLine()) != null) {
                   response.append(readLine +  " " + "\n");
               }

               JSONObject objects = new JSONObject(response.toString());

               userID = objects.getString("uid");
               System.out.println(userID);
               auth = objects.getString("Auth");
               System.out.println(auth);
               token = objects.getString("Token");
               System.out.println(token);

               JSONArray userAcounts = objects.getJSONArray("Accounts");

               for (int i = 0;i< userAcounts.length();i++){
                   JSONObject myStreamingAcounts = userAcounts.getJSONObject(i);
                   accountType += myStreamingAcounts.getString("Type") + ";";
                   System.out.println(accountType);
                   accountID += myStreamingAcounts.getString("AccountID") + ";";
                   System.out.println(accountID);

               }

               //outputStream.flush();
               //outputStream.close();
               //inputReader.close();
           } catch (final Exception e) {e.printStackTrace();

               runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       Toast.makeText(MainActivity.this,
                               "error - server not responding" + e.getMessage().toString(),
                               Toast.LENGTH_LONG).show();
                   }
               });
                      }
       }

       public void sendGet() throws IOException, Exception{

           try{
               String url = "https://api.serverroom.net/wowza/account/23171/liveapps";

               URL obj = new URL(url);
               HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
               con.setRequestMethod("GET");
               con.setRequestProperty("X-Auth-Token",token);

               int responseCode = con.getResponseCode();

               BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
               String inputLine;
               StringBuffer response = new StringBuffer();

               while ((inputLine = in.readLine()) != null) {
                   response.append(inputLine);
               }
               in.close();

               JSONObject object = new JSONObject(response.toString());
               JSONArray jsonArray = object.getJSONArray("LiveApps");

               for (int i = 0; i < jsonArray.length(); i++){
                   JSONObject acountDetails = jsonArray.getJSONObject(i);
                   accountPassword += acountDetails.getString("Password");
                   System.out.println(accountPassword);
                   streamingURL += acountDetails.getString("URL");
                   System.out.println(streamingURL);
                   accountUsername += acountDetails.getString("Username");
                   System.out.println(accountUsername);
               }

           } catch(IOException e) {e.printStackTrace();}
       }

出于某种原因,Loggly正在截断Request.Body属性的右括号之前的所有内容。我尝试删除引号并替换&#34;:&#34;与&#34; =&#34;。知道为什么会出现这个错误或者如何修复?

1 个答案:

答案 0 :(得分:0)

Request.Body数组中的元素是您发送的字符串中唯一有效的json。 Loggly只会解析输入字符串的那一部分,因为事件的其余部分实际上不是json。

相关问题