简体   繁体   English

android http请求在设备上不起作用,在模拟器中有效

[英]android http request doesn't work on device, in emulator works

I know there was an other post like this but it doesn't help. 我知道还有其他类似的帖子,但没有帮助。

My Code is working in an emulator but on the device I get an Exception: java.lang.IllegalArgumentException: Host name may not be null 我的代码在模拟器中运行,但是在设备上却出现异常:java.lang.IllegalArgumentException:主机名不能为null

Permissions are set: 权限设置:

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

And here is the code: 这是代码:

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

}

Every time the HttpGet will be executed the Exception comes up BUT only on the device . 每次执行HttpGet时,仅在设备上出现异常。

Try using: 尝试使用:

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

After you do that look in the uri object in the debugger. 完成后,在调试器中查找uri对象。 check if it is valid. 检查它是否有效。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过android仿真器发送HTTP POST请求不起作用 - Sending an HTTP POST request through the android emulator doesn't work HTTP在Android模拟器中不起作用 - HTTP doesn't work in Android emulator Phonegap:我的Javascript代码可在浏览器上使用,但不能在Android模拟器或设备上使用 - Phonegap: My Javascript code works on browsers but it doesn't work on Android emulator or device ImageView加载问题:在模拟器中可用,在实际设备中不可用 - ImageView loading issue: works in emulator, doesn't work in a real device 自定义listView设计在设备上不起作用,但在模拟器中起作用 - Custom listView design doesn't work on device, but works in emulator textCapSentences适用于模拟器,但不适用于真实设备 - textCapSentences works on emulator but doesn't work on real device Android-OpenGL ES 2.0:仿真器(Works)-设备(没有) - Android - OpenGL ES 2.0 : Emulator (Works) - Device (Doesn't) 声音在Android设备上无效。 在模拟器上正常工作 - Sound won't work on android device. Works fine on emulator 内部服务器中的Android名称解析不起作用(设备和模拟器) - Android name resolution in internal server doesn't work (device and emulator) CORS + Android Webview,不适用于设备(​​但适用于模拟器) - CORS + Android Webview, doesn't work on device (but does on emulator)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM