conn.connect (); Android Studio

1

Good, I'm creating my first android application that is a job for the university, I've been trying for several days to solve a connection problem with a url, 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 my url and the exception always jumps me in conn.connect(); Could you give me some solution?

    
asked by p90 04.04.2016 в 20:15
source

4 answers

1

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

 conn.connect();

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

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

If you have defined the permission, you must ensure that the url is formed correctly, since the bundle may not bring the specified string and you may 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"));
...
...
...
    
answered by 04.04.2016 в 21:33
0

As you comment the other answer, when you are going to make the connection, remember that you have to request Internet permission in 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 in 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 get more information on how to use this class .

Greetings.

    
answered by 05.04.2016 в 08:38
0

You have to use AsynkTask so that there is no conflict 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);
  }
    
answered by 05.05.2016 в 16:34
0

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 it and the closing of the application is forced.

It would be nice to know what exception throws you because I'm sure it tells you that very clearly. As you are told, in addition to the permissions you have to execute it in a thread that is not the main one.

    
answered by 05.05.2016 в 17:21