0

I am facing one problem with android accessing ATG sessionconfirmationNumber using rest API, I am using http urlconnection to achive the same, Code is working perfectly and I am getting output also, but the result in the Json response is rounded as the number is large. below are the code.

 try {
        StrictMode.ThreadPolicy policy = new StrictMode.
                ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        // Defined URL  where to send data
        //      URL url = new URL("http://10.201.62.27:8080/Test-Servlet/TestServletAndroid?"+data);
        URL url = new URL("http://52.70.41.98:7203/rest/model/atg/rest/SessionConfirmationActor/getSessionConfirmationNumber");


        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        HttpURLConnection httpConnection = (HttpURLConnection) conn;
        Log.e("Connection", conn.toString());
        httpConnection.setRequestMethod("GET");
        httpConnection.setRequestProperty("Content-Type", "text/plain");

        httpConnection.connect();


        // Get the server response

        reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while ((line = reader.readLine()) != null) {
            // Append server response in string
            sb.append(line + "\n");

        }
        response = sb.toString();
        Log.v("ResponseVALID", response);
        JSONObject json = new JSONObject(response);
        Log.v("Tag0", json.toString());
        scn = json.getLong("sessionConfirmationNumber");


        /*String srr[] = response.split(":");
        for(int i =0;i<srr.length;i++){
            Log.v("TAG2:",srr[i]);
        }
        SCN=srr[1].replace("}", "");
        SCN= SCN.trim();
        Log.v("SCN ",SCN);
        scn=Long.parseLong(SCN);*/

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (Exception ex) {
        }
    }

I have faced the same problem with angularjs on the web-portal, XMLHttp request helped me out to achieve the goal.

Can I use XMLHttp request from android studio?, if yes!, Please give me some reference as I have tried and nothing got worked out for me.

Any help would be appreciated.

bated
  • 960
  • 2
  • 15
  • 29
Mrinal Deo
  • 81
  • 2
  • 10

1 Answers1

0

I believe the line:

httpConnection.setRequestProperty("Content-Type", "text/plain");

need to be:

httpConnection.setRequestProperty("Content-Type", "application/json");

bated
  • 960
  • 2
  • 15
  • 29
  • See [this](http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type) thread. – bated Jun 08 '16 at 20:32