0

Please have a look at the following code of my android application which sends Json strings contained in strings.xml to web service:

public class MainActivity extends AppCompatActivity {
private WebView browser;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    browser = (WebView) findViewById(R.id.webkit);
    try {
        URL pageURL = new URL(getString(R.string.url));
        trustAllHosts();
        HttpURLConnection con = (HttpURLConnection)pageURL.openConnection();

        String jsonString = getString(R.string.json);

        JsonThread myTask = new JsonThread(this, con, jsonString);
        Thread t1 = new Thread(myTask, "JSON Thread");

        t1.start();

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

/**
 * This is based on the code provided at https://stackoverflow.com/questions/995514/https-connection-android/1000205#1000205
 * Trust every server - dont check for any certificate
 */
private void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[] {};
        }

        public void checkClientTrusted(X509Certificate[] chain,
                                       String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain,
                                       String authType) throws CertificateException {
        }
    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection
                .setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

class JsonThread implements Runnable
{
    private AppCompatActivity activity;
    private HttpURLConnection con;
    private String jsonPayLoad;

    public JsonThread(AppCompatActivity activity, HttpURLConnection con, String jsonPayload)
    {
        this.activity = activity;
        this.con = con;
        this.jsonPayLoad = jsonPayload;
    }

    @Override
    public void run()
    {

        String response = "";
        if (prepareConnection()) {
            response = postJson();
        } else {
            response = "Error preparing the connection";
        }
        showResult(response);
    }


    private void showResult(String response) {
        activity.runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                String page = generatePage(response);
                ((MainActivity)activity).browser.loadData(page, "text/html", "UTF-8");
            }
        });
    }

    private String postJson() {
        String response = "";
        try {
            String postParameters = "jsonpayload=" + URLEncoder.encode(jsonPayLoad, "UTF-8");
            con.setFixedLengthStreamingMode(postParameters.getBytes().length);
            PrintWriter out = new PrintWriter(con.getOutputStream());
            out.print(postParameters);
            out.close();
            int responseCode = con.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                response = readStream(con.getInputStream());
            } else {
                response = "Error contacting server: " + responseCode;
            }
        } catch (Exception e) {
            response = e.toString();//"Error executing code";
        }
        return response;
    }

    private String readStream(InputStream in) {
        StringBuilder sb = new StringBuilder();
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
            String nextLine = "";
            while ((nextLine = reader.readLine()) != null) {
                sb.append(nextLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    private String generatePage(String content) {
        return "<html><body><p>" + content + "</p></body></html>";
    }


    private boolean prepareConnection() {
        try {
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            return true;

        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        return false;
    }
}}

What I need to do here is to print the JSON that I'm sending to LogCat to see that the correct JSON is prepared and sent. How can I do this and where do I try to log it?

0 Answers0