ENETUNREACH(网络无法访问)错误?我在做什么?

时间:2016-03-18 19:37:32

标签: java android server httpurlconnection

我正在尝试与android和我的服务器建立连接。我的服务器发送HTML5响应。

我的主要活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //atempting connection to sever
        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    SimpleHTTPRequest connect=new SimpleHTTPRequest();
                    connect.attemptConnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我的网络课程

public class SimpleHTTPRequest {
HttpURLConnection connection = null;
BufferedReader rd = null;
StringBuilder sb = null;
String line = null;

public void attemptConnect() {

    try {
        URL serverAddress = new URL("myUrl");
        //set up out communications stuff
        connection = null;

        //Set up the initial connection
        connection = (HttpURLConnection) serverAddress.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        //get the output stream writer and write the output to the server
        //not needed in this example
        //wr = new OutputStreamWriter(connection.getOutputStream());
        //wr.write("");
        //wr.flush();
        //read the result from the server
        rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        sb = new StringBuilder();

        while ((line = rd.readLine()) != null) {
            sb.append(line + '\n');
            System.out.println(sb.toString());
            //This will return the String Server Sent event from the server
            //Need to parse the data as JSON Now
            //attempt to parse string from sb=StringBuilder into a JSon object
        }



    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //close the connection, set all objects to null
        connection.disconnect();
        rd = null;
        sb = null;
        connection = null;
    }
}
}

我在eclipse中做到了这一点并且有效,所以我在做什么呢?

0 个答案:

没有答案