Android:未知主机异常

时间:2013-06-03 13:11:24

标签: android web-services soap https

我正在开发一个应用程序,我在其中调用SOAP Web服务。 Web服务部署在本地计算机上,位于独立的OC4J服务器上。

当我从浏览器调用Web服务时,它工作正常。但是当我从我的应用程序中调用它时,它会给我错误。

调用Web服务的代码如下:

package com.abc.myapp.webservice;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.ocs.manageyourgroup.parsing.DataHandler;

public class WebServiceConnection {
final String NAMESPACE = "http://myapp.abc.com/";
final String URL = "http://localmachine:8888/MyApp-MyApplication-context-root/MyAppSoapHttpPort";   

String xml = null;

public void soapRequest(){
    String SOAP_ACTION = "http://myapp.abc.com/getApplicationName";
    String METHOD_NAME = "getApplicationName";
    //Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);        
    //Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {              
        androidHttpTransport.debug = true;
        //this is the actual part that will call the  
        androidHttpTransport.call(SOAP_ACTION, envelope);
        // Get the SoapResult from the envelope body.
        xml = androidHttpTransport.responseDump;

        Log.d("Data", xml);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

我 当我运行我的应用程序时,我在logcat中收到错误:

06-01 18:26:42.094: D/dalvikvm(19946): GC_EXTERNAL_ALLOC freed 59K, 52% free 2584K/5379K, external 716K/1038K, paused 50ms
06-01 18:26:44.413: D/szipinf(19946): Initializing inflate state
06-01 18:26:45.153: W/System.err(19946): java.net.UnknownHostException: localmachine
06-01 18:26:45.208: W/System.err(19946):    at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
06-01 18:26:45.213: W/System.err(19946):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
06-01 18:26:45.213: W/System.err(19946):    at java.net.InetAddress.getAllByName(InetAddress.java:256)
06-01 18:26:45.225: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
06-01 18:26:45.225: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
06-01 18:26:45.233: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
06-01 18:26:45.233: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
06-01 18:26:45.233: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
06-01 18:26:45.247: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
06-01 18:26:45.253: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
06-01 18:26:45.253: W/System.err(19946):    at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
06-01 18:26:45.253: W/System.err(19946):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
06-01 18:26:45.253: W/System.err(19946):    at com.ocs.manageyourgroup.webservice.WebServiceConnection.soapRequest(WebServiceConnection.java:38)
06-01 18:26:45.253: W/System.err(19946):    at com.ocs.manageyourgroup.webservice.WebServiceConnection.getApplicationName(WebServiceConnection.java:23)
06-01 18:26:45.253: W/System.err(19946):    at com.ocs.manageyourgroup.controllers.SplashScreenActivityController.getApplicationName(SplashScreenActivityController.java:24)
06-01 18:26:45.263: W/System.err(19946):    at com.ocs.manageyourgroup.activities.SplashScreenActivity$AsyncLoadDatabase.onPostExecute(SplashScreenActivity.java:54)
06-01 18:26:45.263: W/System.err(19946):    at com.ocs.manageyourgroup.activities.SplashScreenActivity$AsyncLoadDatabase.onPostExecute(SplashScreenActivity.java:1)
06-01 18:26:45.263: W/System.err(19946):    at  android.os.AsyncTask.finish(AsyncTask.java:417)
06-01 18:26:45.263: W/System.err(19946):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-01 18:26:45.263: W/System.err(19946):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-01 18:26:45.263: W/System.err(19946):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 18:26:45.263: W/System.err(19946):    at android.os.Looper.loop(Looper.java:123)
06-01 18:26:45.273: W/System.err(19946):    at android.app.ActivityThread.main(ActivityThread.java:3683)
06-01 18:26:45.273: W/System.err(19946):    at java.lang.reflect.Method.invokeNative(Native Method)
06-01 18:26:45.273: W/System.err(19946):    at java.lang.reflect.Method.invoke(Method.java:507)
06-01 18:26:45.273: W/System.err(19946):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-01 18:26:45.283: W/System.err(19946):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-01 18:26:45.283: W/System.err(19946):    at dalvik.system.NativeStart.main(Native Method)
06-01 18:26:45.333: W/System.err(19946): java.net.UnknownHostException: localmachine
06-01 18:26:45.333: W/System.err(19946):    at java.net.InetAddress.lookupHostByName(InetAddress.java:497)
06-01 18:26:45.333: W/System.err(19946):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
06-01 18:26:45.333: W/System.err(19946):    at java.net.InetAddress.getAllByName(InetAddress.java:256)
06-01 18:26:45.333: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
06-01 18:26:45.333: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
06-01 18:26:45.343: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
06-01 18:26:45.343: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
06-01 18:26:45.343: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
06-01 18:26:45.343: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
06-01 18:26:45.343: W/System.err(19946):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
06-01 18:26:45.343: W/System.err(19946):    at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
06-01 18:26:45.353: W/System.err(19946):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
06-01 18:26:45.353: W/System.err(19946):    at com.ocs.manageyourgroup.webservice.WebServiceConnection.soapRequest(WebServiceConnection.java:38)
06-01 18:26:45.353: W/System.err(19946):    at com.ocs.manageyourgroup.webservice.WebServiceConnection.getApplicationName(WebServiceConnection.java:23)
06-01 18:26:45.353: W/System.err(19946):    at com.ocs.manageyourgroup.controllers.SplashScreenActivityController.getApplicationName(SplashScreenActivityController.java:24)
06-01 18:26:45.353: W/System.err(19946):    at com.ocs.manageyourgroup.activities.SplashScreenActivity$AsyncLoadDatabase.onPostExecute(SplashScreenActivity.java:55)
06-01 18:26:45.353: W/System.err(19946):    at com.ocs.manageyourgroup.activities.SplashScreenActivity$AsyncLoadDatabase.onPostExecute(SplashScreenActivity.java:1)
06-01 18:26:45.353: W/System.err(19946):    at android.os.AsyncTask.finish(AsyncTask.java:417)
06-01 18:26:45.353: W/System.err(19946):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-01 18:26:45.353: W/System.err(19946):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-01 18:26:45.363: W/System.err(19946):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 18:26:45.363: W/System.err(19946):    at android.os.Looper.loop(Looper.java:123)
06-01 18:26:45.363: W/System.err(19946):    at android.app.ActivityThread.main(ActivityThread.java:3683)
06-01 18:26:45.363: W/System.err(19946):    at java.lang.reflect.Method.invokeNative(Native Method)
06-01 18:26:45.363: W/System.err(19946):    at java.lang.reflect.Method.invoke(Method.java:507)
06-01 18:26:45.363: W/System.err(19946):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-01 18:26:45.363: W/System.err(19946):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-01 18:26:45.363: W/System.err(19946):    at dalvik.system.NativeStart.main(Native Method)
06-01 18:26:47.613: D/dalvikvm(19946): GC_EXTERNAL_ALLOC freed 130K, 52% free 2633K/5379K, external 1359K/1828K, paused 49ms
06-01 18:26:47.944: D/dalvikvm(19946): GC_EXTERNAL_ALLOC freed 10K, 52% free 2630K/5379K, external 2181K/2471K, paused 60ms

我在谷歌搜索,发现我需要在应用程序中插入INTERNET权限。但它已经给出了。

我现在应该怎么做。请指导我这个。我在谷歌搜索但没有得到任何好的解决方案。

2 个答案:

答案 0 :(得分:0)

如果要从模拟器访问localhost,则需要使用主机环回接口10.0.2.2,因为模拟器保留了127.0.0.1

请尝试使用此网址:"http://10.0.2.2:8888/MyApp-MyApplication-context-root/MyAppSoapHttpPort"

更多信息:http://developer.android.com/tools/devices/emulator.html#emulatornetworking

答案 1 :(得分:0)

错误的相关部分是java.net.UnknownHostException: localmachine这表示您的手机无法确定主机localmachine与IP地址之间的关系。

因此,除非您能够修改手机的/etc/hosts文件,否则您必须使用localmachine的IP地址而不是其主机名。