Gian Lorenzo Abaño Gian Lorenzo Abaño - 4 months ago 16
Android Question

How to send JSON raw via POST Request on Android API Level 22

Currently I'am debugging my Java Code as follows:

public void sign_in(View view) {
String json = "";

// The Username & Password
final EditText em = (EditText) findViewById(R.id.Username);
String email = (String) em.getText().toString();
final EditText pw = (EditText) findViewById(R.id.Password);
String password = (String) pw.getText().toString();
// -----------------------


JSONObject jsonObject = new JSONObject();
try {
HttpResponse response;
jsonObject.accumulate("email", email);
jsonObject.accumulate("password", password);
json = jsonObject.toString();
URL url = new URL("http://cloudspecinc.herokuapp.com/api/user/login/");
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url.toURI());
httpPost.setEntity(new StringEntity(json, "UTF-8"));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
httpPost.setHeader("Accept-Language", "en-US");
response = httpClient.execute(httpPost);
String sresponse = response.getEntity().toString();
}
catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());

} finally {
/* nothing to do here */
}

}


My question:


  1. How can I get the response of the POST Request? It's not yet in the
    code because I don't know how to get it.

  2. The severe problem: When i click the "LOG IN" button the app always
    crashes. (NOTE: It compiles on android studio but crashes
    when I click the "LOG IN" button that trigger the sign in function).



Something that I think the problem is:


  1. HttpClient is deprecated in Android API Level 22. (Android 5.1
    Lollipop API Level 22)

  2. Something is wrong with the code.


Answer

OK Now I know.. Requesting HTTP Type Request must be on a AsyncTask. not on the current thread because it will throw an exception when not on a Asynchronous Class due to Network Threading Exception. I forgot what the Exception is called. I'am only a beginner on Java. because I'am not really a Java programmer.

Here is the code to help others on this kind of problem.

public class REST extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        HttpURLConnection urlConnection=null;
        String json = null;
        // The Username & Password
        final EditText em =  (EditText) findViewById(R.id.Username);
        String email = (String) em.getText().toString();
        final EditText pw =  (EditText) findViewById(R.id.Password);
        String password = (String) pw.getText().toString();
        // -----------------------

        try {
            HttpResponse response;
            JSONObject jsonObject = new JSONObject();
            jsonObject.accumulate("email", email);
            jsonObject.accumulate("password", password);
            json = jsonObject.toString();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://cloudspecinc.herokuapp.com/api/user/login/");
            httpPost.setEntity(new StringEntity(json, "UTF-8"));
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Accept-Encoding", "application/json");
            httpPost.setHeader("Accept-Language", "en-US");
            response = httpClient.execute(httpPost);
            String sresponse = response.getEntity().toString();
            Log.w("QueingSystem", sresponse);
            Log.w("QueingSystem", EntityUtils.toString(response.getEntity()));
        }
        catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());

        } finally {
        /* nothing to do here */
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (result != null) {
            // do something
        } else {
            // error occured
        }
    }
}