简体   繁体   English

将Android设备连接到PC时异常连接被拒绝

[英]Exception Connection refused while connecting Android device to PC

This i the code of Connection 这是连接的代码

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DL_URL = "http://localhost:8080/project-server/rest/?value=test";
    downloadJsonFile();

}

 private void downloadJsonFile() {
        try {
            createFileAndDirectory();
            URL url = new URL(DL_URL);
            HttpURLConnection urlConnection;
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();


            int status = urlConnection.getResponseCode();

            Log.v("gg", Integer.toString(status));
            if (status == HttpURLConnection.HTTP_OK) {
            FileOutputStream fileOutput = new FileOutputStream(jsonFile);
            InputStream inputStream = urlConnection.getInputStream();
            byte[] buffer = new byte[1024];
            int bufferLength = 0;
            while ((bufferLength = inputStream.read(buffer)) > 0) {
            fileOutput.write(buffer, 0, bufferLength);
            }
            fileOutput.close();
            }else{
                 Toast.makeText(MainActivity.this,"error :)", Toast.LENGTH_LONG).show();

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

        }

And this is my servles located @my own PC tha I try to connect to it (I use Windows 7) 这是我的伺服器,位于我自己的PC上,我尝试连接到它(我使用Windows 7)

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 }

Erros I get 我知道了

    06-18 12:42:55.940: W/System.err(4740): java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
    06-18 12:42:55.950: W/System.err(4740):     at java.net.Socket.connect(Socket.java:983)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
    06-18 12:42:55.950: W/System.err(4740):     at com.example.test1.MainActivity.downloadJsonFile(MainActivity.java:42)
    06-18 12:42:55.950: W/System.err(4740):     at com.example.test1.MainActivity.onCreate(MainActivity.java:30)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
    06-18 12:42:55.950: W/System.err(4740):     at android.os.Handler.dispatchMessage(Handler.java:99)
    06-18 12:42:55.950: W/System.err(4740):     at android.os.Looper.loop(Looper.java:130)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread.main(ActivityThread.java:3835)
    06-18 12:42:55.950: W/System.err(4740):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-18 12:42:55.950: W/System.err(4740):     at java.lang.reflect.Method.invoke(Method.java:507)
    06-18 12:42:55.950: W/System.err(4740):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
    06-18 12:42:55.950: W/System.err(4740):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
    06-18 12:42:55.960: W/System.err(4740):     at dalvik.system.NativeStart.main(Native Method)

You are trying to access http://localhost:8080/project-server/rest/?value=test , but localhost refers to the loopback address 127.0.0.1, so your request is sent to your Android device itself, not to your pc. 您尝试访问http://localhost:8080/project-server/rest/?value=test ,但是localhost指的是回送地址127.0.0.1,因此您的请求将发送到您的Android设备本身,而不是您的PC 。 Change localhost with the ip address of your pc. 用您的PC的IP地址更改localhost。

Try to connect using "10.0.2.2:8080" instead of using "localhost:8080" 尝试使用“ 10.0.2.2:8080”而不是“ localhost:8080”进行连接

It'll work... 会...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM