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){
String serviceURL = "http://localhost:6158/Host/Service1.svc/";
serviceURL += webMethod;
HttpClient client=new DefaultHttpClient();
HttpGet getRequest=new HttpGet();
try {
getRequest.setURI(new URI(serviceURL));
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
BufferedReader in=null;
HttpResponse response=null;
try {
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());
}
return buff.toString();
}
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)