내 로그인 활동에서 android.os.NetworkOnMainThreadException이 표시됩니다. 기본적으로 나는 사용자가 입력 한 사용자 ID와 암호가 올바른지, 즉 사용자가 로그인 할 수 있는지 여부를 확인하려고합니다. 나는 해결책을 찾고 그 예외가 메인 스레드에서 네트워킹과 관련이 있다는 것을 알아 냈다. 하지만 AsyncTask를 사용하고 있습니다. 나는 아직도이 예외를 받고있다. 여기 내 LoginActivity.java이다android.os.NetworkOnMainThreadException 안드로이드 액티비티에서 데이터베이스에 액세스하려고합니다. Android Studio
public class LoginActivity extends AppCompatActivity {
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_user_check_login = "##########################/android_connect/db_connect.php";
int success=0;
String id="";
String password="";
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Defining onClickListener for Login Button
Button loginBtn=(Button) findViewById(R.id.login_btn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Check credentials
EditText phone=(EditText)findViewById(R.id.phone_txt);
EditText pwd=(EditText)findViewById(R.id.password_txt);
id=phone.getText().toString();
password=pwd.getText().toString();
new CheckUserLogin().execute();
if (success == 1) {
Intent intent=new Intent(getApplicationContext(), RideDetailsActivity.class);
startActivity(intent);
}else{
// product with id not found
}
}
});
}
class CheckUserLogin extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_user_check_login, "GET", params);
// check your log for json response
// Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
}
}
나는 이것이 중요하지만 디버깅을 시도하면 알과 doInBackground
방법 내부에 브레이크 포인트를 넣어하지만 내 디버거 그냥 브레이크 포인트를 무시하고 그냥 새로운 CheckUserLogin().execute();
과거 불면하지 않습니다 성명서.
또한 여기 내 예외 스택 추적이 있습니다.
FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:587)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:489)
at com.heycabs.heycabs.JSONParser.makeHttpRequest(JSONParser.java:65)
at com.heycabs.heycabs.LoginActivity$CheckUserLogin$1.run(LoginActivity.java:98)at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5409)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
RunOnUiThread()로 UI 스레드에서 모든 것을 실행하여 'AsyncTask' 사용의 목적을 완전히 무 찌르십시오. 그래서'NetworkOnMainThreadException'을 얻는 것입니다. –
지적 해 주셔서 고맙습니다. 나는 안드로이드를 처음 사용하고 있으며 가이드로 [this] (http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/) 튜토리얼을 사용하고 있습니다. 내가 무엇을 바꾸어야하는지 알려 주실 수 있습니까? –