<bdo id='XpOTZ'></bdo><ul id='XpOTZ'></ul>
    <legend id='XpOTZ'><style id='XpOTZ'><dir id='XpOTZ'><q id='XpOTZ'></q></dir></style></legend>

      <i id='XpOTZ'><tr id='XpOTZ'><dt id='XpOTZ'><q id='XpOTZ'><span id='XpOTZ'><b id='XpOTZ'><form id='XpOTZ'><ins id='XpOTZ'></ins><ul id='XpOTZ'></ul><sub id='XpOTZ'></sub></form><legend id='XpOTZ'></legend><bdo id='XpOTZ'><pre id='XpOTZ'><center id='XpOTZ'></center></pre></bdo></b><th id='XpOTZ'></th></span></q></dt></tr></i><div id='XpOTZ'><tfoot id='XpOTZ'></tfoot><dl id='XpOTZ'><fieldset id='XpOTZ'></fieldset></dl></div>

      <small id='XpOTZ'></small><noframes id='XpOTZ'>

      <tfoot id='XpOTZ'></tfoot>
      1. 使用 Android HttpURLConnection 获取 Http

        Http Get using Android HttpURLConnection(使用 Android HttpURLConnection 获取 Http)
        • <bdo id='EKdro'></bdo><ul id='EKdro'></ul>
            <tbody id='EKdro'></tbody>
          <tfoot id='EKdro'></tfoot>
          <legend id='EKdro'><style id='EKdro'><dir id='EKdro'><q id='EKdro'></q></dir></style></legend>

          <small id='EKdro'></small><noframes id='EKdro'>

            <i id='EKdro'><tr id='EKdro'><dt id='EKdro'><q id='EKdro'><span id='EKdro'><b id='EKdro'><form id='EKdro'><ins id='EKdro'></ins><ul id='EKdro'></ul><sub id='EKdro'></sub></form><legend id='EKdro'></legend><bdo id='EKdro'><pre id='EKdro'><center id='EKdro'></center></pre></bdo></b><th id='EKdro'></th></span></q></dt></tr></i><div id='EKdro'><tfoot id='EKdro'></tfoot><dl id='EKdro'><fieldset id='EKdro'></fieldset></dl></div>

                1. 本文介绍了使用 Android HttpURLConnection 获取 Http的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 Java 和 Android 开发的新手,我尝试创建一个简单的应用程序,该应用程序应该联系 Web 服务器并使用 http get 将一些数据添加到数据库中.

                  I'm new to Java and Android development and try to create a simple app which should contact a web server and add some data to a database using a http get.

                  当我使用计算机中的网络浏览器拨打电话时,它工作得很好.但是,当我在 Android 模拟器中运行应用程序时,不会添加任何数据.

                  When I do the call using the web browser in my computer it works just fine. However, when I do the call running the app in the Android emulator no data is added.

                  我已将 Internet 权限添加到应用的清单中.Logcat 没有报告任何问题.

                  I have added Internet permission to the app's manifest. Logcat does not report any problems.

                  谁能帮我找出问题所在?

                  Can anyone help me to figure out what's wrong?

                  这里是源代码:

                  package com.example.httptest;
                  
                  import java.io.IOException;
                  import java.net.HttpURLConnection;
                  import java.net.MalformedURLException;
                  import java.net.URL;
                  import android.app.Activity;
                  import android.os.Bundle;
                  import android.util.Log;
                  import android.widget.TextView;
                  
                  public class HttpTestActivity extends Activity {
                      @Override
                      public void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                  
                          TextView tv = new TextView(this);
                          setContentView(tv);
                  
                          try {
                              URL url = new URL("http://www.mysite.se/index.asp?data=99");
                              HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                              urlConnection.disconnect();
                              tv.setText("Hello!");
                          }
                          catch (MalformedURLException ex) {
                              Log.e("httptest",Log.getStackTraceString(ex)); 
                          }
                          catch (IOException ex) {
                              Log.e("httptest",Log.getStackTraceString(ex));
                          }   
                      }        
                  }
                  

                  推荐答案

                  尝试从这里获取输入流,然后可以得到文本数据:-

                  Try getting the input stream from this you can then get the text data as so:-

                      URL url;
                      HttpURLConnection urlConnection = null;
                      try {
                          url = new URL("http://www.mysite.se/index.asp?data=99");
                  
                          urlConnection = (HttpURLConnection) url
                                  .openConnection();
                  
                          InputStream in = urlConnection.getInputStream();
                  
                          InputStreamReader isw = new InputStreamReader(in);
                  
                          int data = isw.read();
                          while (data != -1) {
                              char current = (char) data;
                              data = isw.read();
                              System.out.print(current);
                          }
                      } catch (Exception e) {
                          e.printStackTrace();
                      } finally {
                          if (urlConnection != null) {
                              urlConnection.disconnect();
                          }    
                      }
                  

                  您也可以使用其他输入流阅读器,例如缓冲阅读器.

                  You can probably use other inputstream readers such as buffered reader also.

                  问题是当您打开连接时 - 它不会拉"任何数据.

                  The problem is that when you open the connection - it does not 'pull' any data.

                  这篇关于使用 Android HttpURLConnection 获取 Http的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Lucene Porter Stemmer not public(Lucene Porter Stemmer 未公开)
                  How to index pdf, ppt, xl files in lucene (java based or python or php any of these is fine)?(如何在 lucene 中索引 pdf、ppt、xl 文件(基于 java 或 python 或 php 中的任何一个都可以)?)
                  KeywordAnalyzer and LowerCaseFilter/LowerCaseTokenizer(KeywordAnalyzer 和 LowerCaseFilter/LowerCaseTokenizer)
                  How to search between dates (Hibernate Search)?(如何在日期之间搜索(休眠搜索)?)
                  How to get positions from a document term vector in Lucene?(如何从 Lucene 中的文档术语向量中获取位置?)
                  Java Lucene 4.5 how to search by case insensitive(Java Lucene 4.5如何按不区分大小写进行搜索)
                      <bdo id='9PsZN'></bdo><ul id='9PsZN'></ul>
                          <i id='9PsZN'><tr id='9PsZN'><dt id='9PsZN'><q id='9PsZN'><span id='9PsZN'><b id='9PsZN'><form id='9PsZN'><ins id='9PsZN'></ins><ul id='9PsZN'></ul><sub id='9PsZN'></sub></form><legend id='9PsZN'></legend><bdo id='9PsZN'><pre id='9PsZN'><center id='9PsZN'></center></pre></bdo></b><th id='9PsZN'></th></span></q></dt></tr></i><div id='9PsZN'><tfoot id='9PsZN'></tfoot><dl id='9PsZN'><fieldset id='9PsZN'></fieldset></dl></div>

                            <tbody id='9PsZN'></tbody>
                          <tfoot id='9PsZN'></tfoot>

                          <legend id='9PsZN'><style id='9PsZN'><dir id='9PsZN'><q id='9PsZN'></q></dir></style></legend>
                          • <small id='9PsZN'></small><noframes id='9PsZN'>