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:
Something that I think the problem is:
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
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.