2015-04-04 183 views
0

我使用「xampp」構建一個web服務器,並在C:\ xampp \ htdocs上創建「abc.php」。Android無法連接網絡服務器

完成所有工作後,我鏈接「localhost/abc.php」。

使用PC谷歌來竄改URL可以顯示「這是服務器的消息」。

到目前爲止是好的。直到我想android連接網絡服務器。

我嘗試了很多方法(包括關閉防火牆或防病毒等),但仍然失敗。

我試過谷歌(android手機)連接「http://MyIP/abc.php」,不通過這個程序。

結果是可以在谷歌(Android手機)中顯示我的「abc.php」。

但我不知道哪裏錯誤。 我不能用我的程序獲取URL

package com.testdb; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    private TextView textView; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textView = (TextView)findViewById(R.id.textView1); 
     db message = new db(); 
     String msg = message.stringQuery("http://10.0.2.2/abc.php"); 
     textView.setText("Server message is "+msg); 
    } 

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

} 

-

package com.testdb; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 

public class db { 

    public String stringQuery(String url){ 
     try 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost method = new HttpPost(url); 
      HttpResponse response = httpclient.execute(method); 
      HttpEntity entity = response.getEntity(); 
      if(entity != null){ 
       return EntityUtils.toString(entity); 
      } 
      else{ 
       return "No string."; 
      } 
     } 
     catch(Exception e){ 
      return "Network problem"; 
     } 
    } 
} 

,這是我的日誌

04-04 16:30:07.425: W/Trace(1183): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:16.906: E/Trace(1301): error opening trace file: No such file or directory (2) 
04-04 16:30:16.906: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:16.916: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:16.916: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:17.006: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:17.006: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:17.796: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:17.796: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:17.816: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:17.836: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.046: D/libEGL(1301): loaded /system/lib/egl/libEGL_emulation.so 
04-04 16:30:18.066: D/(1301): HostConnection::get() New Host Connection established 0x2a0ac400, tid 1301 
04-04 16:30:18.106: D/libEGL(1301): loaded /system/lib/egl/libGLESv1_CM_emulation.so 
04-04 16:30:18.126: D/libEGL(1301): loaded /system/lib/egl/libGLESv2_emulation.so 
04-04 16:30:18.289: W/EGL_emulation(1301): eglSurfaceAttrib not implemented 
04-04 16:30:18.309: D/OpenGLRenderer(1301): Enabling debug mode 0 
04-04 16:30:18.309: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.316: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.316: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.376: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.396: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.486: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.689: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.716: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
04-04 16:30:18.786: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0 
+0

您的Android手機是否連接到了您的WiFi網絡,或者您是否嘗試通過蜂窩連接連接到該網址? – yole 2015-04-04 17:49:00

+0

我想通過蜂窩連接連接到該URL,而不是連接WIFI網絡工作 – 2015-04-04 18:46:27

+0

不建議用簡單的第一個字母創建類名。 – 2015-04-04 18:55:49

回答

0

10.0.2.2是本地網絡上的IP地址。它無法從互聯網訪問,因此具有蜂窩連接的Android手機無法連接到它。您需要在公共託管服務上運行Web應用程序,而不是在本地計算機上運行。

+0

是的,我知道。實際上我使用固定IP連接。 – 2015-04-04 19:10:53

+0

如果您的本地機器具有固定的外部IP地址,則需要在Android應用程序中使用該地址。 10.0.2.2不是外部IP地址。 – yole 2015-04-04 19:12:09

+0

我確定我的固定IP是一個有效的,因爲我試過其他機器連接到。實際上我總是連接到「MyFixedIP/abc.php」電話 – 2015-04-04 19:14:32

相關問題