0

我知道还有其他类似的帖子,但它没有帮助。

我的代码在模拟器中运行,但在设备上出现异常:java.lang.IllegalArgumentException:主机名可能不为空

权限设置:

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

这是代码:

package com.example.testapp;

import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;

public class mainAct extends Activity {

    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        ARequestTask task = new ARequestTask();

        task.execute(new String[] {});

    }

    class ARequestTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {

            String hallo = "test";
            Log.d("EMAIL", hallo);

            try {
                HttpClient client = new DefaultHttpClient();
                URI uri = new URI("http://.../android/index.php");
                HttpGet get = new HttpGet(uri);

                HttpResponse responseGET = client.execute(get);
                HttpEntity resEntity = responseGET.getEntity();

                if (resEntity != null) {
                    Log.i("RESPONSE", EntityUtils.toString(resEntity));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(String result) {
            // use the result as you wish
        }
    }

}

每次执行 HttpGet 时,都会出现异常,但仅在设备上出现。

4

1 回答 1

0

尝试使用:

URI uri = URI.parse("http://.../android/index.php");

完成后查看调试器中的 uri 对象。检查它是否有效。祝你好运。

于 2012-11-15T09:21:46.870 回答