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

时间:2013-06-18 11:48:55

标签: java android http url

这是我的连接代码

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

        }

这是我的servles位于@my自己的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

    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)

2 个答案:

答案 0 :(得分:8)

您正在尝试访问http://localhost:8080/project-server/rest/?value=test,但localhost指的是环回地址127.0.0.1,因此您的请求会发送到您的Android设备本身,而不是您的电脑。使用您的电脑的IP地址更改localhost。

答案 1 :(得分:2)

尝试使用“10.0.2.2:8080”进行连接,而不是使用“localhost:8080”

它会起作用......