4

我有这个代码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());           
        }

这是我的清单:

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

这是我的日志:

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

非常感谢!!此错误的常见原因不包括“ http://”,但我正在这样做!

我也试过HttpGet httpGet = new HttpGet(URL); 和相同的 URL,但带有 post 和 put。

4

2 回答 2

3

不完全确定为什么,但如果你更换

new HttpGet(new URI(URL));

经过

new HttpGet(URL);

你不应该有这个问题。

编辑

这不是问题。正如您在评论中建议的那样,显然不支持“_”:

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

显然你真的不应该在你的主机名中加上下划线。

于 2013-07-30T13:31:29.640 回答
0

虽然已经回答了,但我有另一个解决方案。我有同样的问题,并通过"http://"在 url 之前添加来解决它。奇怪的是,它在没有它的情况下工作了一年左右。

于 2014-11-30T09:46:26.677 回答