The host name may not be null, including HTTP in Android

I have this code in 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 list:

  <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>

Here is 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

Thank you. Common causes of this error do not include "http: / /", but I'm doing it!

I also tried httpget httpget = new httpget (URL); And the same URL, but with post and put

resolvent:

Not entirely sure why, but if you replace

new HttpGet(new URI(URL));

adopt

new HttpGet(URL);

You shouldn't have this problem

edit

It's not a problem. Obviously you don't support '', As you suggested in your comments:

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

Obviously, you really shouldn't underline the host name

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>