Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在尝试在模拟器上获取Android应用程序_Android_Http_Android Emulator - Fatal编程技术网

正在尝试在模拟器上获取Android应用程序

正在尝试在模拟器上获取Android应用程序,android,http,android-emulator,Android,Http,Android Emulator,我正在尝试开发一个Android应用程序,可以连接到互联网并从指定的网站读取数据。但是,当我在模拟器上运行程序时,什么都没有发生。有人能帮我做些什么吗?这是我的密码 public class HttpExaplme extends Activity { TextView httpStuff; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我正在尝试开发一个Android应用程序,可以连接到互联网并从指定的网站读取数据。但是,当我在模拟器上运行程序时,什么都没有发生。有人能帮我做些什么吗?这是我的密码

public class HttpExaplme extends Activity {
TextView httpStuff;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpex);
    httpStuff = (TextView) findViewById(R.id.tv_http);
    GetHttpEx test = new GetHttpEx();
    String returned;
    try {
        returned = test.getInternetData();
        httpStuff.setText(returned);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public class GetHttpEx {

public String getInternetData() throws Exception
{
    BufferedReader in = null;
    String data = null;
    try{
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://www.mybringback.com");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l ="";
        String nl= System.getProperty("line sperator");
        while((l = in.readLine())!=null)
        {
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();
                return data;
       }
    finally
    {
        try{
            if(in != null)
                {
                    in.close();
                    return data;
                }
        }
        catch(Exception e){
            e.printStackTrace();
        }

    }
}

您正在崩溃,因为您正在UI线程上执行HTTP请求。这是不允许的-你会使你的应用程序没有响应,所以它会抛出一个异常。所有HTTP请求都必须在线程或异步任务中发出。

首先,检查仿真器是否连接到。通过在android浏览器中打开此站点来完成此操作。默认情况下,公司防火墙可能会阻止仿真器请求

其次,正如@Gabe所说的,将您的网络请求包装在一个异步任务中。在这里:

完成这些步骤后回到这里。请贴一张stacktrace。您可以在logcat中查看它(命令行上的adb-elogcat)


编辑:您是否在清单中设置了android.permission.INTERNET?

在命令提示符下(假设您有路径),运行以下命令以查看日志(并查看错误):adb logcatFrom Honeycom版本的android它是强制创建的。UI线程在complsory上工作在UI线程上,非UI线程在非UI线程上工作。此外,如果您只是简单地调试和测试一点,您可以通过关闭manifest.xml中的targetSdk来关闭此检查,因为旧版本不做此检查。当然,你们不应该真的启动这样的应用程序。