java - HttpResponse response = client.execute(httpGet),httpresponse后面的代码没有运行

标签 java php android

我尝试从我的网站 /http://localhost/android_connection/getHttpGet.php 获取数据,该数据遵循此站点的代码 http://www.thaicreate.com/mobile/android-httpget-httppost.html

遵循此行后“/http://localhost/android_connection/getHttpGet.php”的结果:

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

我正在为我的本地主机使用 Xampp。

在android编程中 >> HttpResponse response = client.execute(httpGet) 之后的代码不会通过捕获 log.e 来运行。 Log.e("error1.1","尝试之前");和 Log.e("error1.2","HttpResponse 之前");只出现在我的LogCat上。

我不知道如何修复,请帮助我,顺便说一下,我是通过 Android 应用程序使用网络服务器进行编程的初学者。

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();
    }
}

最佳答案

问题在于您的服务器网址,Android 设备无法识别本地主机网址,因为这是您机器的网址。我个人建议您通过计算机 IP 访问 XAMP 服务器,但如果您正在使用模拟器并希望使用本地主机,只需将 url 更改为 10.0.2.2。请参阅developer guide

关于java - HttpResponse response = client.execute(httpGet),httpresponse后面的代码没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24028581/

相关文章:

php - 如何防止在 PHP 中使用超出 "use"范围的特征方法

php - 使用 PHP 检查 Azure AD 中的用户凭据

php - 如何制作一个简单的 php 应用程序从 heroku cedar stack 发送电子邮件?

android - 防止按返回按钮取消操作模式

java - 如何调试servlet 404

java - 带间隔的 JPQL 查询日期

java - 自定义组件不显示

android - float :left stops click event on android?

android - react native Android : how to remove "overscroll effect" of ScrollView

java - eclipse maven 循环占位符引用错误使用配置文件