【问题标题】:HttpResponse response = client.execute(httpGet), the code after httpresponse doesn't runHttpResponse response = client.execute(httpGet),httpresponse之后的代码没有运行
【发布时间】:2014-06-04 03:37:22
【问题描述】:

我尝试从我的网站/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。

在安卓编程中 >> HttpResponse response = client.execute(httpGet) 之后的代码不会通过捕获 log.e 来运行。 Log.e("error1.1","尝试前");和 Log.e("error1.2","Before 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();
    }
}

【问题讨论】:

  • 我认为这条线有一个例外HttpResponse response = client.execute(httpGet);请检查你的logcat并更新你的问题
  • 我想你会得到 NetworkOnMainThread 异常。如果是,在 AsyncTask 中调用 getHttpGet() 或 new Thread(){@Override public void run(){//code here}}.start();
  • QAMAR 能否请您写下如何在 AsyncTask 中调用 getHttpGet(),因为我是本主题的新手,我不知道 AsyncTask 是如何工作和编写的。非常感谢,什么是 NetworkOnMainThread 异常?
  • 在logcat中有06-04 W/System.err(4309): org.apache.http.conn.HttpHostConnectException: Connection to localhost denied >>> 是不是代码的问题?

标签: java php android


【解决方案1】:

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

【讨论】:

  • 很多人提到错误是因为NetworkOnMainThreadException,但这是不可能的,因为代码中实施了严格的策略。
  • 我使用的是真机,LG G2。我认为我的服务器没问题,因为我可以在 chrome 浏览器上运行该 url,也可以在 localhost/phpmyadmin 上管理我的数据库。顺便说一句,现在我正在研究 AsyncTask,就像上面的建议所说的那样。
  • 你能从你的 LG G2 设备打开服务器吗?我认为甚至您的设备都不可能识别什么是本地主机? Aysnc 任务是您绝对应该实施的一件事,但这不是解决此问题的方法。
  • 它适用于真机和动车组。我所做的只是改变 String url = "localhost/android_connection/getHttpGet.php"; >>>> String url = "http//***你的计算机的运行网络服务器的IP***/android_connection/getHttpGet.php";
  • 是的,我从一开始就是这么说的,虽然很高兴它奏效了,但仍然不知道为什么人们不赞成它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-07
相关资源
最近更新 更多