0

this is my code i have run my code in main thread, is there any way to implement checking server on click of the search button action.I am getting error "android.os.NetworkOnMainThreadException" . Due to this error i could not proceed further.

public class MainActivity extends AppCompatActivity implements
        View.OnClickListener {

    //region fields
    private Button dateTextView;

    RestUrl restUrl;
    String searchValue;

    //endregion

    //region onCreate
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         //region search action
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchValue = mSearchView.getText().toString();
                if (searchValue.length() < 1) {
                    Toast.makeText(MainActivity.this, "Input value to search", Toast.LENGTH_SHORT).show();
                } else {
                    if(isURLReachable(MainActivity.this))
                    {
                        Intent list = new Intent(MainActivity.this,HotelListActivity.class);
                        list.putExtra("searchValue",searchValue);
                        startActivity(list);
                    }
                    else {
                        Toast.makeText(MainActivity.this, "Turn on WIFI or MOBILE DATA", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        //endregion
    }
    //endregion

    ***checking url method***
     static public boolean isURLReachable(Context context) {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected()) {
                try {
                    URL url = new URL(RestUrl.REST_SERVICE);   // Change to "http://google.com" for www  test.
                    HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                    urlc.setConnectTimeout(10 * 1000);          // 10 s.
                    urlc.connect();
                    if (urlc.getResponseCode() == 200) {        // 200 = "OK" code (http connection is fine).
                        Log.wtf("Connection", "Success !");
                        return true;
                    } else {
                        return false;
                    }
                } catch (MalformedURLException e1) {
                    return false;
                } catch (IOException e) {
                    return false;
                }
            }
            return false;
        }

    }
  • 2
    Possible duplicate of [How to fix android.os.NetworkOnMainThreadException?](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) – 0xDEADC0DE Nov 25 '16 at 07:29
  • 1
    you should use AsynTask or Runnable thread to hit the server at background till that you can show the progress dialog – Raju Nov 25 '16 at 07:32
  • but i have to return a boolean value . Could it be possible to return in runnable thread? – Sachit karki Nov 25 '16 at 07:50

0 Answers0