在Android中无法连接http

时间:2012-10-21 15:33:50

标签: java android emulation httpconnection

这段代码在java中工作得很好但是当我开始在Android 4.0模拟器上运行它时崩溃了。虽然调试我注意到它在httpConn.connect();线

  public class GetStringFromUrl {
public static String getString(String urlPageAdress) throws Exception
{

    URL url = new URL(urlPageAdress);
     HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
      urlConn.setRequestProperty("Accept-Encoding", "UTF-8");
     HttpURLConnection httpConn = (HttpURLConnection) urlConn;
      httpConn.setAllowUserInteraction(false);
     httpConn.connect(); //crashes on this line dunno know why

     InputStream in = null;

     if (httpConn.getContentEncoding() != null && httpConn.getContentEncoding().toString().contains("gzip")) {
     in = new GZIPInputStream(httpConn.getInputStream());
      } else {
      in = httpConn.getInputStream();
    }
     BufferedInputStream bis = new BufferedInputStream(in);
     ByteArrayBuffer baf = new ByteArrayBuffer(1000);
     int read = 0;
      int bufSize = 1024;
      byte[] buffer = new byte[bufSize];
     while (true) {
      read = bis.read(buffer);
      if (read == -1) {
    break;
      }
     baf.append(buffer, 0, read);



     }
      String body = new String(baf.toByteArray());
    return body;
} }

方法用于主要活动

公共类MainActivity扩展了Activity {

ToggleButton  toogleButton;
 public final static String EXTRA_MESSAGE = "ru.kazartsevaa.table.MESSAGE";
int upDown;
int SpinnerCount;
  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    upDown=0;


    setContentView(R.layout.activity_main);
}

public void onClick(View v)抛出异常     {System.out.print(SpinnerCount);     // if(upDown == 0)// 0 - вылет1прилет         // {             开关(v.getId())             {                     案例R.id.UpDownButton:                     {                         // toogleButton =(ToggleButton)findViewById(R.id.UpDownButton);                     // toogleButton.setOnCheckedChangeListener(this);                     }                     案例R.id.SheremetievoButton:                     {                         Spinner SherSpinner =(Spinner)findViewById(R.id.SheremetievoSpinner);

                    String SpinnerCount= SherSpinner.getItemAtPosition(SherSpinner.getSelectedItemPosition()).toString();
                    System.out.print(SpinnerCount);
                    new Thread(){ public void run() { 
                        try {
                            String body = GetStringFromUrl.getString("www.xyz.com");
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        } }.start();

                        int crowd;






                }

        }

注意:更改为新线程后,它会停止压缩但仍会写入

1 个答案:

答案 0 :(得分:2)

你是在主线程中这样做的吗?如果你在一个单独的线程中执行它,应该可以工作。

    public void onClick(View v) {
    try{
            new Thread(){
                    public void run() {
                        GetStringFromUrl.getString("www.xyz.com");                      
                    }
            }.start();
    }catch(Exception e){
        e.printStackTrace();
    }
}