本文介绍了在发送请求休息Web服务时我得到了例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在浏览器中复制并粘贴HttpGet的getRequest对象的值时,它给出了正确的结果但是当我在Eclipse中通过模拟器运行它时会抛出异常。

提前感谢捆绑如果有任何想法的话。





When i copy and paste the value of getRequest object of HttpGet in the browser it gives me the correct result But when i run it in Eclipse by emulator it throws exception.
Bundle of thanks in advance if any idea about it.


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):在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.j ava: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):FATAL例外:主要

06-30 06:18:02.455:E / AndroidRuntime(938):java.lang.NullPointerException

06-30 06:18:02.455:E / AndroidRuntim e(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):at 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 / Andr oidRuntime(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)



The LogCat is as follows :

06-30 06:08:58.332: E/AndroidRuntime(851): FATAL EXCEPTION: main
06-30 06:08:58.332: E/AndroidRuntime(851): android.os.NetworkOnMainThreadException
06-30 06:08:58.332: E/AndroidRuntime(851): at 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): at android.view.View.performClick(View.java:3480)
06-30 06:08:58.332: E/AndroidRuntime(851): at android.view.View$PerformClick.run(View.java:13983)
06-30 06:08:58.332: E/AndroidRuntime(851): at android.os.Handler.handleCallback(Handler.java:605)
06-30 06:08:58.332: E/AndroidRuntime(851): at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 06:08:58.332: E/AndroidRuntime(851): at android.os.Looper.loop(Looper.java:137)
06-30 06:08:58.332: E/AndroidRuntime(851): at 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): FATAL EXCEPTION: main
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): at android.view.View.performClick(View.java:3480)
06-30 06:18:02.455: E/AndroidRuntime(938): at android.view.View$PerformClick.run(View.java:13983)
06-30 06:18:02.455: E/AndroidRuntime(938): at android.os.Handler.handleCallback(Handler.java:605)
06-30 06:18:02.455: E/AndroidRuntime(938): at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 06:18:02.455: E/AndroidRuntime(938): at android.os.Looper.loop(Looper.java:137)
06-30 06:18:02.455: E/AndroidRuntime(938): at 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)

推荐答案




这篇关于在发送请求休息Web服务时我得到了例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:16