HTT presponse响应= client.execute(HTTPGET),之后HTT presponse的code不运行 [英] HttpResponse response = client.execute(httpGet), the code after httpresponse doesn't run

查看:235
本文介绍了HTT presponse响应= client.execute(HTTPGET),之后HTT presponse的code不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试从我的网站获取数据 /http://localhost/android_connection/getHttpGet.php 其中按照code从这个网站,< A HREF =htt​​p://www.thaicreate.com/mobile/android-httpget-httppost.html相对=nofollow> http://www.thaicreate.com/mobile/android-httpget-httppost.html 。

的结果 /http://localhost/android_connection/getHttpGet.php 只是跟随这行之后:

 服务器日期/时间:2014年6月4日4时28分49秒

我使用的XAMPP的我的本地主机。

在机器人编程
在code之后>>的Htt presponse响应= client.execute(HTTPGET)不捕log.e.运行Log.e(error1.1,先试);和Log.e(error1.2,型Htt presponse之前);只出现在我的LogCat中。

我不知道如何解决plz帮助我,顺便我是在通过Android应用程序使用网络服务器的编程初学者。

 公共类MainActivity延伸活动{    @燮pressLint(NewApi)
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //权限StrictMode
        如果(android.os.Build.VERSION.SDK_INT&GT; 9){
            。StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建();
            StrictMode.setThreadPolicy(政策);
        }        最终的TextView结果=(的TextView)findViewById(R.id.result);
        最终按钮btnGet =(按钮)findViewById(R.id.btnGet);        btnGet.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
                // TODO自动生成方法存根
                字符串URL =HTTP://localhost/android_connection/getHttpGet.php
                字符串resultServer = getHttpGet(URL);
                result.setText(resultServer);
            }
        });
    }    公共字符串getHttpGet(字符串URL){        StringBuilder的海峡=新的StringBuilder();
        HttpClient的客户端=新DefaultHttpClient();
        HTTPGET HTTPGET =新HTTPGET(URL);
        Log.e(error1.1,先试);
        尝试{
            Log.e(error1.2,型Htt presponse之前);
            HTT presponse响应= client.execute(HTTPGET);
            Log.e(error1.3,中的Htt presponse结束);
            状态行状态行= response.getStatusLine();
            INT状态code = statusline.getStatus code();
            如果(状态code == 200){//状态OK
                HttpEntity实体= response.getEntity();
                InputStream的内容= entity.getContent();
                读者的BufferedReader =新的BufferedReader(新的InputStreamReader(内容));
                串线;
                而((行= reader.readLine())!= NULL){
                    str.append(线);
                    Log.e(误差2,行+线);
                }
            }
        }赶上(ClientProtocolException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        返回str.toString();
    }
}


解决方案

问题是与你的服务器的URL,因为这是你的机器的网址Android设备不认本地主机地址。我个人建议你用你的机器的IP来访问你的服务器XAMP,但如果您正在使用模拟器并希望与本地主机的工作只更改URL为10.0.2.2。请参阅开发指南

I've try to get data from my web /http://localhost/android_connection/getHttpGet.php which follow the code from this site,http://www.thaicreate.com/mobile/android-httpget-httppost.html.

The result of "/http://localhost/android_connection/getHttpGet.php" just after following this line :

Server Date/Time : 2014-06-04 04:28:49

I'm using Xampp for my localhost.

In android programming the code after >> HttpResponse response = client.execute(httpGet) doesn't run by catching log.e. Log.e("error1.1","Before try"); and Log.e("error1.2","Before HttpResponse"); only appear on my LogCat.

I don't know how to fix plz help me, by the way i'm a beginner in programming of using webserver via android application.

public class MainActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Permission StrictMode


        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        final TextView result = (TextView) findViewById(R.id.result);
        final Button btnGet = (Button) findViewById(R.id.btnGet);

        btnGet.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String url = "http://localhost/android_connection/getHttpGet.php";
                String resultServer  = getHttpGet(url);
                result.setText(resultServer);
            }
        });
    }

    public String getHttpGet(String url) {

        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        Log.e("error1.1","Before try");
        try {
            Log.e("error1.2", "Before HttpResponse");
            HttpResponse response = client.execute(httpGet);
            Log.e("error1.3", "End of HttpResponse");
            StatusLine statusline = response.getStatusLine();
            int statusCode = statusline.getStatusCode();
            if (statusCode == 200) { // Status OK
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                    Log.e("error2","line : " + line);
                }
            }


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str.toString();
    }
}

解决方案

The problem is with your server url, android devices don't recognize the localhost url as this is your url of the machine. I personally recommend you to access your XAMP server by your machine IP but if you are working on emulator and want to work with local host only change the url to 10.0.2.2. Refer developer guide

这篇关于HTT presponse响应= client.execute(HTTPGET),之后HTT presponse的code不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆