Reputation: 7
I will make a IsLoggedIn Function with PHP (because i check again the name and password in db), but i get a NetworkOnMainThreadException in my logcat. What is wrong?
public boolean LoggedIn() {
try {
int success;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.makeHttpRequest(CHECK_URL, "POST", params);
success = json.getInt(TAG_SUCCESS);
if(success == 1) {
return true;
}
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
Upvotes: 0
Views: 29
Reputation: 700
Like the exception says, you can't run network operations on the UI thread on Android. You will need to run them on a separate thread so your application doesn't freeze while you're doing stuff on the network. The most common approach for this is an AsyncTask.
Upvotes: 1