http请求与其他人一样,但在Android上不起作用

时间:2013-08-06 10:10:21

标签: android http

我正在尝试将网站的内容变成字符串。我在这个网站和其他人上搜索了很长时间以找到一个例子,但都没有用。

我从教程视频http://www.youtube.com/watch?v=TxcfhAU2dDg

中获取了代码

我添加了许可,3G正在运行(其他应用访问互联网并显示图标)。

我也试过一个真实的设备,但没有积极的结果。

我可能只是犯了一个愚蠢的错误。你能帮我找一下我的虫子吗?

我的代码是:

package com.example.testfail;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView httpStuff;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    httpStuff = (TextView) findViewById(R.id.tv);
    GetMethodEx test = new GetMethodEx();
    String returned;
    try {
        returned = test.getInternetData();
        httpStuff.setText(returned);
        httpStuff.setText("ah?");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

和GetMethodEx:

package com.example.testfail;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetMethodEx {

public String getInternetData() throws Exception{

    BufferedReader in = null;
    String data = null;
    try {
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("google.com");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new  InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while((l = in.readLine()) != null){
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();
        return data;
    } finally {
        if(in != null){
            try {
                in.close();
                return data;
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
}

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您正在尝试在UI线程上执行网络任务。这是不允许的。您必须在单独的线程中执行网络请求,即扩展AsyncTask

我会发布代码,只是因为它是一个教程而且我前段时间碰巧看过这个。您可以看到使用了AsyncTask:

public class HTTPGetter extends Activity implements OnClickListener {

TextView tvHttp;
BufferedReader in = null;
EditText ethttp;
Button bhttp;
String returned, strhttp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpex);
    tvHttp = (TextView) findViewById(R.id.tvHTTP);
    bhttp = (Button) findViewById(R.id.bhttp);
    ethttp = (EditText) findViewById(R.id.ethttp);
    bhttp.setOnClickListener(this);
}

private class HTTPGet extends AsyncTask<String, Void, String> {

    BufferedReader in = null;
    String returned = null;

    @Override
    protected String doInBackground(String... params) {

        try {

            HttpClient client = new DefaultHttpClient();

            URI website = new URI(strhttp);

            HttpGet request = new HttpGet();

            request.setURI(website);

            HttpResponse response = client.execute(request);

            in = new BufferedReader(new InputStreamReader(response
                    .getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");

            String l = "";

            String nl = System.getProperty("line.separator");

            while ((l = in.readLine()) != null) {

                sb.append(l + nl);
            }

            in.close();
            returned = sb.toString();

        } catch (ClientProtocolException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } catch (URISyntaxException e) {

            e.printStackTrace();
        }
        return null;
        }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);
        tvHttp.setText(returned);
    }
    }

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.bhttp:

        strhttp = ethttp.getText().toString();
        HTTPGet test = new HTTPGet();

        try {
            test.execute(strhttp);

        } catch (Exception e) {
            e.printStackTrace();
            }
        break;
    }
}
}