Issue
I am trying to create a user and POST the user name and password to the server. The code below runs fine in plain java file, but crashed when run in Android due to
android.os.NetworkOnMainThreadException.
I did some search and was told to use AsyncTask but I don't know how to use AsyncTask, how to execute and passing in parameters. I need to pass in url and account Info both are String.
Below is my code that run in plain java file.
public String sendPost(String accountInfo, String input_url) throws Exception {
// Establish Connection
URL url = new URL(input_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
// Post account info to server
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(accountInfo);
wr.flush();
wr.close();
// Check response code and message
int responseCode = con.getResponseCode();
IntputStream is = con.getErrorStream();
String contentAsString = CovertToString(is, 500);
JSONObject jObject = new JSONObject(contentAsString);
jObject.put("response", responseCode);
return jObject;
}
Below is my attempt to make it into AsyncTask.
class Test extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... params) {
try {
// Establish Connection
URL url = new URL(input_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
// Post account info to server
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(accountInfo);
wr.flush();
wr.close();
// Check response code and message
int responseCode = con.getResponseCode();
IntputStream is = con.getErrorStream();
String contentAsString = CovertToString(is, 500);
JSONObject jObject = new JSONObject(contentAsString);
jObject.put("response", responseCode);
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Don't know how to call the method and pass in parameter, and not sure if I implemented the method correctly.
Appreciate any help in advance, thanks!
Solution
You can do something like this.
class Test extends AsyncTask<String, Void, JSONObject>{
@Override
protected JSONObject doInBackground(String... params) {
JSONObject jObject = null;
try {
String accountInfo = params[0];
String input_url = params[1];
// Establish Connection
URL url = new URL(input_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
// Post account info to server
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(accountInfo);
wr.flush();
wr.close();
// Check response code and message
int responseCode = con.getResponseCode();
InputStream is = con.getErrorStream();
String contentAsString = CovertToString(is, 500);
jObject = new JSONObject(contentAsString);
jObject.put("response", responseCode);
}catch (IOException e) {
e.printStackTrace();
}
return jObject;
}
protected void onPostExecute(JSONObject data) {
// do some things with JSONObject as this runs on UI thread.
}
}
To invoke the task, call
new Test().execute(accountInfo, input_url);
onPostExecute
gets called on the UI thread so you can call your UI operations from there..
See here for more information on AsyncTask.
Answered By - Neo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.