Http Post Request To Ibm Personality Insights With Android
I would like to make an HTTP post request to IBM Service Personality Insight using Android. I tried to use this code: private String mServer='gateway.watsonplatform.net'; private i
Solution 1:
Your problems are quite common as Android beginner. As the log, android.os.NetworkOnMainThreadException
stated that you are doing your network task on main thread which is not allowable. You have to do network task in background otherwise you can't get away from the errors. You can fix this problems by wrapping your current code with AsyncTask
.
privateclassMyAsycnTaskextendsAsyncTask<String, String, String> {
privateString process = null;
protectedStringdoInBackground(String... urls) {
// do your network task in background HttpResponse res = makeRequest(urls[0]);
this.process = processRequest(res);
returngetBodyFromResponse(res);
}
protectedvoidonPostExecute(String responseBody) {
// do your UI task in main threadshowDialog("Downloaded " + result + " bytes");
}
/*
The rest of your code are below here
....
*/
}
You should understand the relationship between background and main thread, or else you can't understand why AsyncTask is needed in networking. For more details about AsyncTask, please take a looks at docs.
http://developer.android.com/reference/android/os/AsyncTask.html
Post a Comment for "Http Post Request To Ibm Personality Insights With Android"