Android的HTTP GET [英] Android HTTP Get

查看:148
本文介绍了Android的HTTP GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看几个论坛的帖子,我无法找到一个答案我的问题。我想从一个PHP文件的响应。 PHP文件的工作。问题是,Android应用程序将不执行我的要求。下面是我的code,结果我得到了TextView的两个例子:

I've look at a few forum post and I can not find an answer to my problem. I am trying to get a response from a php file. The php file is working. The problem is the Android App will not execute my request. Here are two examples of my code and the result I get in the textview:

public void changText(View view) {
    TextView textv = (TextView)findViewById(R.id.textview1);
    textv.setText("Text Has Been Changed");
    BufferedReader in = null;
    String data = null;

    try{
           HttpClient httpclient = new DefaultHttpClient();

           HttpGet request = new HttpGet();
           URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
           request.setURI(website);
           HttpResponse response = httpclient.execute(request);
           in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

           textv.append(" Connected ");
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
       }


   }

TextView的记载:已被修改

The TextView reads: Text Has Been Changed

    public void changText(View view) {
    TextView textv = (TextView)findViewById(R.id.textview1);
    textv.setText("Text Has Been Changed");
    BufferedReader in = null;
    String data = null;

    try{
           HttpClient httpclient = new DefaultHttpClient();

           HttpGet request = new HttpGet();
           URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
           request.setURI(website);
           //HttpResponse response = httpclient.execute(request);
           //in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

           textv.append(" Connected ");
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
       }


   }

TextView的内容如下:文本已被更改连接

The TextView reads: Text Has Been Changed Connected

在此清单中,我有:

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

在错误日志中我得到以下几点: 在HTTP连接android.os.NetworkOnMainThreadException错误

In the error log I get the following: Error in http connection android.os.NetworkOnMainThreadException

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

Android的可能是在执行你的要求就好了。你似乎只是被忽略服务器返回的数据。你可以做这样的事情,例如:

Android is probably executing your request just fine. You just seem to be ignoring the data returned by the server. You could do something like this, for instance:

public void changText(View view) {
    TextView textv = (TextView)findViewById(R.id.textview1);
    textv.setText("Text Has Been Changed");
    BufferedReader in = null;
    String data = null;

    try{
           HttpClient httpclient = new DefaultHttpClient();

           HttpGet request = new HttpGet();
           URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
           request.setURI(website);
           HttpResponse response = httpclient.execute(request);
           in = new BufferedReader(new InputStreamReader(
                   response.getEntity().getContent()));

           // NEW CODE
           String line = in.readLine();
           textv.append(" First line: " + line);
           // END OF NEW CODE

           textv.append(" Connected ");
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
       }
}

它看起来像你的服务器返回一个JSON对象,所以你可能会想要做一些更聪明的,例如读取整个响应,解析它(使用新JSONArray(响应)),并提取相关领域,但上述code将至少验证Android正在执行查询。

It looks like your server is returning a JSON object, so you would probably want to do something more intelligent such as read the entire response, parse it (using new JSONArray(response)), and extract relevant fields, but the above code would at least verify that Android is executing your query.

编辑:从您报告的异常,看来你是主UI线程上运行您的code。由于API 11的禁止(在此之前,在被皱起了眉头)。对于如何解决这一问题的文章。请参阅指南专题无痛线程以及教程的here 和<一href="http://www.techblogistech.com/2011/11/how-to-fix-the-android-networkonmainthreadexception/">here.另请参见这个线程了解详情。

From the exception you report, it appears that you are running your code on the main UI thread. As of API 11 that is prohibited (and was frowned upon before that). There are several articles on how to fix this. See the Guide topic Painless threading as well as tutorials here and here. Also see this thread for more information.

这篇关于Android的HTTP GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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