conn.connect (); Android Studio

Hi, I'm creating my first android application which is a job for the university, I've been trying to fix a connection problem with a url for several days, could you help me?

This Is My Code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_palabra_dificil_to_sinonimo);

    txtSinonimo = (TextView) findViewById(R.id.TxtSinonimo);

    //Recuperamos la información pasada en el intent
    Bundle bundle = this.getIntent().getExtras();

    try {
        URL url = new URL(myURL + URLEncoder.encode(bundle.getString("Palabra"), "UTF-8"));InputStream is;
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        conn.connect();
        is = conn.getInputStream();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Where in myURL I have put my url and the exception always jumps me in conn.connect(); could you give me some solution?

 1
Author: p90, 2016-04-04

4 answers

If the "error", occurs when you try to make the connection,

 conn.connect();

The first question would be, Did you define the connection permission in your AndroidManifest.xml ?

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

If you have defined the permission, you should make sure the url is formed correctly, as the bundle might not bring the specified string and you might be trying to load an incorrect URL:

myURL += URLEncoder.encode(bundle.getString("Palabra");

Log.i("Conexión", "el url es: " + myURL);  //revisa en el LogCat tu Url!.

URL url = new URL(myURL + URLEncoder.encode(bundle.getString("Palabra"), "UTF-8"));
...
...
...
 1
Author: Jorgesys, 2016-04-05 01:21:45

As the other answer says, when you are going to make the connection, remember that you have to request the internet permission in the Android Manifest and you should also check that your URL is well formed, although that is not your problem.
The problem you have is that you are throwing a connection on the main thread, and that is what produces the error.
To perform tasks on a secondary thread you can use a AsyncTask. In this link you can learn more about how to use this class .

A greeting.

 0
Author: Puas, 2016-04-05 06:38:29

You have to use AsynkTask so that no conflict is generated with the main thread of the application, see:

class MyAsynTask extends AsyncTask<Long, Integer, Integer> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected <Type> doInBackground(Long... params) {
        URL ur_url = newURL(http://....) 
               // do the works on url.....
        return <tuped>result;
    }

    @Override
    protected void onPostExecute(Integer result) {
        // set the results in Ui

    }
}

And in your Activity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    StrictMode.ThreadPolicy policy = new       
                  StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
          new MyAsynTask().execute(null, null, null);
  }
 0
Author: Daniel ORTIZ, 2016-05-05 14:34:28

You can not make a connection to the internet in the main thread since you would block the application, in the first versions of Android this was possible but from one version an exception is thrown when trying and the application is forced to close.

It would be nice to know which exception throws you since surely there it tells you very clearly. As they tell you, in addition to the permissions you have to run it on a thread other than the main one.

 0
Author: Alberto Méndez, 2016-05-05 15:21:41