I am creating apps in android in that I have login logout activity, Multilistview activity which data coming from rest web service. here problem is that when calling webservice from login activity I get error the networkonmainthreadexception then i searched on goggle about that exception, somebody said on stack overflow use AsyncTask in your code for seperation thread and I implemented asynctask in my code but not working. i am totally confusing how to use asynctask and I want to add asynctask in following code. I provide code without asynctask which i did. can anybody to help me how to exactly use asynctask when calling webservice.
following is the get data from edit text call function
UserFunctions userFun = new UserFunctions();
if ((username.trim().length() > 0)&&(userpsw.trim().length() > 0)) {
JSONObject json = userFun.loginUser(username, userpsw);
.
.
.
following is the function class
public JSONObject loginUser(String userEmail, String userPsw) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", userEmail));
params.add(new BasicNameValuePair("password", userPsw));
JSONObject json = jsonParser.getJsondataFromUrl(params);
//Log.d("tag", json.toString());
return json;
}
following is the actual webservice class
public void getJsondataFromUrl(List<NameValuePair> params) {
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResp = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResp.getEntity();
inStream = httpEntity.getContent();
//Log.d(tag, inStream.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader bufferReader = new BufferedReader(new InputStreamReader
(inStream, "iso-8859-1"), 8);
StringBuilder strBuilder = new StringBuilder();
String line = null;
while ((line = bufferReader.readLine()) != null) {
strBuilder.append(line + "n");
}
inStream.close();
json = strBuilder.toString();
//Log.d("JSON", json);
} catch (Exception e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jsonObj = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObj;*/
}
Thanks in advance