向Rest Web服务发送请求会抛出异常并在线路上崩溃“response = client.execute(getRequest);”

时间:2014-06-30 01:37:49

标签: java android eclipse android-emulator

当我在浏览器中复制并粘贴HttpGet的getRequest对象的值时,它给出了正确的结果但是当我通过模拟器在Java中运行它时抛出异常。 如果对此有任何想法,请提前致谢。

  public static String callWebService(String webMethod){
        // HTTP get client
  String serviceURL = "http://localhost:6158/Host/Service1.svc/";  
            serviceURL += webMethod; 
            HttpClient client=new DefaultHttpClient();
            HttpGet getRequest=new HttpGet();

            try {
                // construct a URI object
                getRequest.setURI(new URI(serviceURL));
            } catch (URISyntaxException e) {
                Log.e("URISyntaxException", e.toString());
            }

            // buffer reader to read the response
            BufferedReader in=null;
            // the service response
            HttpResponse response=null;
            try {
                // execute the request
                response = client.execute(getRequest);
            } catch (ClientProtocolException e) {
                Log.e("ClientProtocolException", e.toString());
            } catch (IOException e) {
                Log.e("IO exception", e.toString());
            }
            try {
                in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            } catch (IllegalStateException e) {
                Log.e("IllegalStateException", e.toString());
            } catch (IOException e) {
                Log.e("IO exception", e.toString());
            }
            StringBuffer buff=new StringBuffer("");
            String line="";
            try {
                while((line=in.readLine())!=null)
                {
                    buff.append(line);
                }
            } catch (IOException e) {
                Log.e("IO exception", e.toString());
                return e.getMessage();
            }

            try {
                in.close();
            } catch (IOException e) {
                Log.e("IO exception", e.toString());
            }
            // response, need to be parsed
            return buff.toString();
    }

LogCat如下:

06-30 06:08:58.332:E / AndroidRuntime(851):致命异常:主要 06-30 06:08:58.332:E / AndroidRuntime(851):android.os.NetworkOnMainThreadException 06-30 06:08:58.332:E / AndroidRuntime(851):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084) 06-30 06:08:58.332:E / AndroidRuntime(851):at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:74) 06-30 06:08:58.332:E / AndroidRuntime(851):at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 06-30 06:08:58.332:E / AndroidRuntime(851):at libcore.io.IoBridge.connect(IoBridge.java:112) 06-30 06:08:58.332:E / AndroidRuntime(851):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 06-30 06:08:58.332:E / AndroidRuntime(851):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 06-30 06:08:58.332:E / AndroidRuntime(851):at java.net.Socket.connect(Socket.java:842) 06-30 06:08:58.332:E / AndroidRuntime(851):at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 06-30 06:08:58.332:E / AndroidRuntime(851):at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) 06-30 06:08:58.332:E / AndroidRuntime(851):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 06-30 06:08:58.332:E / AndroidRuntime(851):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 06-30 06:08:58.332:E / AndroidRuntime(851):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 06-30 06:08:58.332:E / AndroidRuntime(851):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 06-30 06:08:58.332:E / AndroidRuntime(851):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 06-30 06:08:58.332:E / AndroidRuntime(851):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 06-30 06:08:58.332:E / AndroidRuntime(851):at com.rest.application.Globals.callWebService(Globals.java:49) 06-30 06:08:58.332:E / AndroidRuntime(851):at com.rest.application.RestAppActivity $ 1.onClick(RestAppActivity.java:39) 06-30 06:08:58.332:E / AndroidRuntime(851):在android.view.View.performClick(View.java:3480) 06-30 06:08:58.332:E / AndroidRuntime(851):在android.view.View $ PerformClick.run(View.java:13983) 06-30 06:08:58.332:E / AndroidRuntime(851):在android.os.Handler.handleCallback(Handler.java:605) 06-30 06:08:58.332:E / AndroidRuntime(851):在android.os.Handler.dispatchMessage(Handler.java:92) 06-30 06:08:58.332:E / AndroidRuntime(851):在android.os.Looper.loop(Looper.java:137) 06-30 06:08:58.332:E / AndroidRuntime(851):在android.app.ActivityThread.main(ActivityThread.java:4340) 06-30 06:08:58.332:E / AndroidRuntime(851):at java.lang.reflect.Method.invokeNative(Native Method) 06-30 06:08:58.332:E / AndroidRuntime(851):at java.lang.reflect.Method.invoke(Method.java:511) 06-30 06:08:58.332:E / AndroidRuntime(851):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 06-30 06:08:58.332:E / AndroidRuntime(851):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-30 06:08:58.332:E / AndroidRuntime(851):at dalvik.system.NativeStart.main(Native Method) 06-30 06:18:02.316:E / Exception(938):android.os.NetworkOnMainThreadException 06-30 06:18:02.455:E / AndroidRuntime(938):致命异常:主要 06-30 06:18:02.455:E / AndroidRuntime(938):java.lang.NullPointerException 06-30 06:18:02.455:E / AndroidRuntime(938):at com.rest.application.Globals.callWebService(Globals.java:59) 06-30 06:18:02.455:E / AndroidRuntime(938):at com.rest.application.RestAppActivity $ 1.onClick(RestAppActivity.java:39) 06-30 06:18:02.455:E / AndroidRuntime(938):在android.view.View.performClick(View.java:3480) 06-30 06:18:02.455:E / AndroidRuntime(938):在android.view.View $ PerformClick.run(View.java:13983) 06-30 06:18:02.455:E / AndroidRuntime(938):在android.os.Handler.handleCallback(Handler.java:605) 06-30 06:18:02.455:E / AndroidRuntime(938):在android.os.Handler.dispatchMessage(Handler.java:92) 06-30 06:18:02.455:E / AndroidRuntime(938):在android.os.Looper.loop(Looper.java:137) 06-30 06:18:02.455:E / AndroidRuntime(938):在android.app.ActivityThread.main(ActivityThread.java:4340) 06-30 06:18:02.455:E / AndroidRuntime(938):at java.lang.reflect.Method.invokeNative(Native Method) 06-30 06:18:02.455:E / AndroidRuntime(938):at java.lang.reflect.Method.invoke(Method.java:511) 06-30 06:18:02.455:E / AndroidRuntime(938):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 06-30 06:18:02.455:E / AndroidRuntime(938):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-30 06:18:02.455:E / AndroidRuntime(938):at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案