2012-11-25 81 views
1

我試圖建立起來,最終通過web服務將Android應用程序連接到數據庫。作爲一箇中間步驟,我試圖看看我能否正常工作。但是,當我複製/粘貼並使用textview設置適當的佈局文件時,嘗試運行時出現錯誤。具體來說,我的代碼如下所示(我試圖從同一臺機器上的仿真器運行,因此10.0.2.2)任何指針將不勝感激!Android PHP連接錯誤

package com.example.componenttest; 

import android.app.Activity; 
import android.os.Bundle; 
import java.io.IOException; 
import android.widget.TextView; 
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 AndroidWebActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("10.0.2.2/InterfaceTest/GrabMe.php"); 
     try { 
      TextView tv = (TextView) findViewById(R.id.tv1); 
     //tv.setText("CHANGED TEXT"); 
      HttpResponse response = httpclient.execute(httppost); 
      final String str = EntityUtils.toString(response.getEntity()); 
      tv.setText(str); 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

我的錯誤日誌如下所示:

11-24 22:30:14.976: D/dalvikvm(7269): GC_EXTERNAL_ALLOC freed 53K, 48% free 2807K/5379K, external 1687K/2079K, paused 221ms 
11-24 22:30:17.890: D/AndroidRuntime(7269): Shutting down VM 
11-24 22:30:17.890: W/dalvikvm(7269): threadid=1: thread exiting with uncaught exception (group=0x4001e578) 
11-24 22:30:17.921: E/AndroidRuntime(7269): FATAL EXCEPTION: main 
11-24 22:30:17.921: E/AndroidRuntime(7269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.componenttest/com.example.componenttest.AndroidWebActivity}: java.lang.IllegalStateException: Target host must not be null, or set in parameters. 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at android.os.Looper.loop(Looper.java:130) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at java.lang.reflect.Method.invoke(Method.java:507) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at dalvik.system.NativeStart.main(Native Method) 
11-24 22:30:17.921: E/AndroidRuntime(7269): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:572) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at com.example.componenttest.AndroidWebActivity.onCreate(AndroidWebActivity.java:24) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
11-24 22:30:17.921: E/AndroidRuntime(7269):  ... 11 more 
+0

這是一個SOAP還是REST Web服務? –

回答

0

嘗試改變

HttpPost httppost = new HttpPost("10.0.2.2/InterfaceTest/GrabMe.php"); 

HttpPost httppost = new HttpPost("http://10.0.2.2/InterfaceTest/GrabMe.php"); 

我高我建議使用nuSOAP PHP庫來創建一個SOAP服務器。也許可以嘗試一些更具體的Android客戶端,比如這個庫:https://code.google.com/p/droidsoapclient/。如果您打算使用的SOAP是...... =)

+0

哇,謝謝!這是一個簡單的解決方案!正確的時候我發佈了這個,我開始再次查看我的代碼,並意識到它可能需要'http://'。感謝您的確認並感謝您的快速響應 - 非常感謝! – Chimi