Android HTTP连接和流"收集"

时间:2014-04-20 18:10:49

标签: android eclipse http

我"写了#34;一些应该执行http请求+流下载的代码,但它似乎不适用于第二部分。它连接到服务器,但没有从中获取预期的数据。它开始的最初几次,它显示了来自谷歌的HTML数据,但之后,  我试图优化我的代码并丢弃一些东西,在这里和那里添加一些东西,通过类等扩展它,这可能导致"错误"。这是我的代码:

public class MainActivity extends ActionBarActivity {

private static final String TAG = null;
TextView httpStuff;
public static String  info;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    try {
        AsyncDownload data = (AsyncDownload) new AsyncDownload().execute();
        info = data.get();
        httpStuff.setText(info);
    } catch (Exception e){
        e.printStackTrace();
    }

}


public class AsyncDownload extends AsyncTask<Void, Void, String> {


    protected String doInBackground(Void... params) {

        Log.v(TAG, "Final Requsting URL is : :" + "google.com");

        String line = "";
        String responseData = null;

        try {
            StringBuilder sb = new StringBuilder();
            String x = "";
            URL httpurl = new URL("http://www.google.com");
            URLConnection  tc = httpurl.openConnection();

            BufferedReader in = new BufferedReader(
                           new InputStreamReader(tc.getInputStream()));

            while ((line = in.readLine()) != null) {
                sb.append(line + "\n");
                x = sb.toString();
            }
            responseData = new String(x);
        }
        catch (UnknownHostException uh){            
            Log.v("NewWebHelper", "Unknown host :");
            uh.printStackTrace();
        }
       catch (FileNotFoundException e) {
           Log.v("NewWebHelper", "FileNotFoundException :");
            e.printStackTrace();
         }
        catch (IOException e) {
            Log.v("NewWebHelper", "IOException :");
            e.printStackTrace();
        }
      catch (Exception e) {
          Log.v("NewWebHelper", "Exception :");
            e.printStackTrace();
        }
        return responseData;
    }

}

我只是不知道丢失的东西是什么。我已经尝试使用各种各样的数据传递版本来解决这个问题,但似乎没有任何工作。我指望你,brainiacs。

0 个答案:

没有答案