Morckovka
Morckovka

Reputation: 103

Failed to http Connect in Android

Well this code works fine in java but when i started to run it on Android 4.0 emulator it crashes. While Debugging i noticed that it crushed on httpConn.connect(); line

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

method is used in Main Activity

public class MainActivity extends 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) throws Exception { System.out.print(SpinnerCount); // if(upDown==0) //0 - вылет 1 прилет //{ switch (v.getId()) { case R.id.UpDownButton: { //toogleButton = (ToggleButton) findViewById(R.id.UpDownButton); // toogleButton.setOnCheckedChangeListener(this); } case 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;






                }

        }

Note: After changing to new thread it stopped crushing but still writes

Upvotes: 1

Views: 894

Answers (1)

13hsoj
13hsoj

Reputation: 204

Are you doing this in the main thread? Should work if you execute this in a separate thread.

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

Upvotes: 2

Related Questions