4

I have this code in an AsyncTask:

try 
        {
            Log.d("API", "URL: "+URL.trim());   
            StringBuilder builder = new StringBuilder();
            HttpClient client = new DefaultHttpClient();        
            HttpGet httpGet = new HttpGet(new URI(URL));
            httpGet.setHeader("Accept","application/json");
            httpGet.setHeader("Content-Type","application/json");


            Log.d("API", "URL Host: "+httpGet.getURI().getHost());
            Log.d("API", "URL Path: "+httpGet.getURI().getPath());

            Log.d("API", "URL: "+URL.trim());       

                HttpResponse response = client.execute(httpGet);
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();
                if (statusCode == 200) 
                {
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                    String line;
                    while ((line = reader.readLine()) != null) 
                    {
                        builder.append(line);
                    }
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("id","2"));       
                    if ( builder.toString().length()>0)
                    {                               
                        Log.v("API", builder.toString());   
                    }
                    else
                    {
                        Log.e("API", "Respuesta vacia");    
                    }
                } 
                else 
                {
                    Log.e("API", "Status code != 200");
                } 

        }
        catch(Exception e)
        {
            e.printStackTrace();
            Log.e("API", "Error >> "+e.getMessage());           
        }

Here is my manifest:

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.apiestacionamientomedido.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

And here my log:

07-30 13:21:10.717: D/API(807): URL: http://test_sem.mardelplata.gob.ar/ServerRest/Cliente/Login?Usuario=123&Clave=123&format=json
07-30 13:21:11.677: D/API(807): URL Host: null
07-30 13:21:11.727: D/API(807): URL Path: /ServerRest/Cliente/Login
07-30 13:21:11.727: D/API(807): URL: http://test_sem.mardelplata.gob.ar/ServerRest/Cliente/Login?Usuario=123&Clave=123&format=json
07-30 13:21:11.727: W/System.err(807): java.lang.IllegalArgumentException: Host name may not be null
07-30 13:21:11.787: W/System.err(807):  at org.apache.http.HttpHost.<init>(HttpHost.java:83)
07-30 13:21:11.837: W/System.err(807):  at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
07-30 13:21:11.847: W/System.err(807):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-30 13:21:11.847: W/System.err(807):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-30 13:21:11.847: W/System.err(807):  at com.example.apiestacionamientomedido.MainActivity.descargarFecha(MainActivity.java:108)
07-30 13:21:11.847: W/System.err(807):  at com.example.apiestacionamientomedido.MainActivity$MiTareaSplash.doInBackground(MainActivity.java:75)
07-30 13:21:11.847: W/System.err(807):  at com.example.apiestacionamientomedido.MainActivity$MiTareaSplash.doInBackground(MainActivity.java:1)
07-30 13:21:11.847: W/System.err(807):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-30 13:21:11.847: W/System.err(807):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-30 13:21:11.847: W/System.err(807):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-30 13:21:11.847: W/System.err(807):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-30 13:21:11.847: W/System.err(807):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-30 13:21:11.847: W/System.err(807):  at java.lang.Thread.run(Thread.java:1096)
07-30 13:21:11.887: E/API(807): Error >> Host name may not be null
07-30 13:21:11.957: W/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45165168

Thanks a lot!! The common reason for this error is not including "http://", but I'm doing that!

I have also tried HttpGet httpGet = new HttpGet(URL); and the same URL but with post and put.

benoffi7
  • 3,068
  • 3
  • 29
  • 44
  • 1
    It's posible that Android doesnt support "_" ? If I try another URL, the code works and if I try the URL in Chrome, its works – benoffi7 Jul 30 '13 at 13:31
  • http://stackoverflow.com/a/8768928/833647 It's the underscore, I just tested. http://en.wikipedia.org/wiki/Hostname "a hostname may not contain other characters, such as the underscore character (_)" Interesting! I did not know that. – Ken Wolf Jul 30 '13 at 13:35
  • You should alwyas encode your URL, try using `URLEncoder.encode(url);` – Inon Stelman Jul 30 '13 at 13:39

2 Answers2

3

Not entirely sure why, but if you replace

new HttpGet(new URI(URL));

by

new HttpGet(URL);

You should not have this issue.

Edit

This is not the issue. Apparently the '_' is not supported, as you suggested in your comment :

https://issues.apache.org/jira/browse/HTTPCLIENT-911

Apparently you really are not supposed to put an underscore in your host name.

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • android may not include the version which resolves the issue. in any case, you can try with URLHttpConnection, and I recommend that you remove the _ from the url – njzk2 Jul 30 '13 at 14:20
  • Where do you I use URLHttpConnection? The url is not mine – benoffi7 Jul 30 '13 at 14:26
  • URLHttpConnection is another http client available on android (recommended for gingerbread and above) that probably doesn't use the same url parser, and therefore may have a different behaviour – njzk2 Jul 30 '13 at 15:01
0

Although it's been answered already, I have another solution. I had the same issue and resolved it by adding "http://" before the url. Strangely it worked a year or so without it.

serj
  • 508
  • 4
  • 20