连接拒绝了android

时间:2014-01-12 16:02:52

标签: android connection

我正在尝试连接从Android设备在我的计算机上分配的服务器。 两者都连接在同一网络中。 我的电脑的IP:192.168.1.135 在港口听:9000

import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        //setContentView(R.layout.activity_main);

                new Thread(new Runnable() {
                    public void run() {

                        try {

                            HttpClient httpclient = new DefaultHttpClient();
                            HttpPost httppost = new HttpPost("http://192.168.1.135:9000/application/login");

                            // Add your data
                            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                            nameValuePairs.add(new BasicNameValuePair("username", "timestron"));
                            nameValuePairs.add(new BasicNameValuePair("password", "00000"));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                            // Execute HTTP Post Request
                            HttpResponse response = httpclient.execute(httppost);

                              readStream(response.getEntity().getContent());
                              } catch (Exception e) {
                              e.printStackTrace();
                            }           
                    }
                }).start();


    ///////////////////////////////////-fin oncreate    
    }

    private void readStream(InputStream in) {
          BufferedReader reader = null;
          try {
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
              System.out.println(line);
            }
          } catch (IOException e) {
            e.printStackTrace();
          } finally {
            if (reader != null) {
              try {
                reader.close();
              } catch (IOException e) {
                e.printStackTrace();
                }
            }
          }
        } 



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

我添加了句子以获取在manifest.xml中连接INTERNET的权限

错误:

01-12 16:52:59.165: W/System.err(8406): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.135:9000 refused
01-12 16:52:59.165: W/System.err(8406):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
01-12 16:52:59.165: W/System.err(8406):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-12 16:52:59.165: W/System.err(8406):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-12 16:52:59.165: W/System.err(8406):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-12 16:52:59.165: W/System.err(8406):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
01-12 16:52:59.165: W/System.err(8406):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
01-12 16:52:59.165: W/System.err(8406):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

1 个答案:

答案 0 :(得分:0)

很明显,这不是任何路由器 - 防火墙相关的问题,因为你在同一个网络下,所以只有三种可能性:

  1. 该IP
  2. 上的那个端口没有任何内容
  3. 该计算机上有一个阻止该连接尝试的本地防火墙
  4. 您没有使用WIFI,因此您不在同一个网络下。
  5. 您可以手动从计算机中的浏览器打开该URL吗?如果是的话,我建议使用一些调试工具来跟踪TCP数据包(我不知道你在目标机器上使用什么样的操作系统;如果它是一些linux发行版,tcpdump可能有帮助。)< / p>

    假设您在AndroidManifest.xml文件中拥有android.permission.INTERNET权限。