安卓HTTP Get

4

我看了一些论坛帖子,但没有找到解决我的问题的答案。我正在尝试从一个 php 文件中获取响应。该 php 文件是正常工作的。问题在于 Android 应用程序无法执行我的请求。以下是我的代码示例和文本视图中获得的结果:

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


   }

文本视图显示:文本已更改
    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显示的是:文本已更改连接

在这个清单文件中,我有:

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

在错误日志中我看到如下信息: 网络连接错误 android.os.NetworkOnMainThreadException
需要帮助请告知。

在你发布的代码中,一旦构建了读取器,你实际上并没有读取数据。 - Ted Hopp
1
如果你运行你的代码会发生什么? - Thein
我现在不是想读取返回的JSON对象,我只是想连接到我的PHP文件。 - user908759
如果我使用第一个示例运行代码,TextView 将显示:“文本已更改”。如果我使用第二个示例运行代码,TextView 将显示:“文本已更改已连接”。似乎是 httpclient.execute(request); 导致了问题。 - user908759
2个回答

14

Android 可能已经成功执行了您的请求。您只是似乎忽略了服务器返回的数据。例如,您可以这样做:

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对象,所以你可能想要做一些更智能的事情,比如读取整个响应,并解析它(使用new JSONArray(response)),并提取相关字段,但上面的代码至少可以验证Android正在执行你的查询。

编辑:从你报告的异常中看来,似乎你正在主UI线程上运行代码。从API 11开始禁止这样做(在那之前也是不被赞同的)。有几篇文章介绍了如何解决这个问题。请参阅指南主题Painless threading,以及这里这里的教程。另请参见此主题获取更多信息。


要将整个响应读入String中,请查看此答案 - Ted Hopp
没有添加Connected。这就是我举两个例子的原因。当我注释掉“httpclient.execute(request);”时,Connected被添加到TextView中,而当“httpclient.execute(request);”没有被注释掉时,Connected没有被添加。这就是为什么我认为问题与“httpclient.execute(request);”有关的原因。 - user908759
PID: 1854 TID: 1854 标签: 跟踪 文本: nativeGetEnabled 返回意外值 标签: 0 AND PID: 1854 TID: 1854 标签: log_tag 文本: http连接错误 android.os.NetworkOnMainThreadException - user908759
1
啊,问题在于您正在主线程上执行此代码。这是不允许的。有几篇文章介绍如何解决这个问题。请参阅指南主题Painless threading,以及教程herehere。还可以查看this thread - Ted Hopp
似乎这就是问题所在,你能编辑你的回答吗,这样我就可以给你解决方案的点数了。 - user908759
显示剩余5条评论

0
 private InputStream downloadUrl(final URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(NET_READ_TIMEOUT_MILLIS /* milliseconds */);
    conn.setConnectTimeout(NET_CONNECT_TIMEOUT_MILLIS /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    return conn.getInputStream();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接