I am trying to call the rest api web service from an android application. I have created a class extending AsyncTask that makes the rest call Following is the code
package com.example.app;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.StrictMode;
@SuppressLint("NewApi") public class ApiAuthRestGet extends AsyncTask<String
, String, String> {
static String username = "admin";
static String password = "Admin123";
static String URLBase = "www.openmrs/ws/rest/v1/";
/**
* HTTP POST
* @param URLPath
* @param input
* @return
* @throws Exception
*/
//public static Boolean getRequestPost(String URLPath, String input)
throws Exception {}
/**
* HTTP GET
* @param URLPath
* @return
* @throws Exception
*/
public static void setUsername(String username) {
ApiAuthRestPost.username = username;
}
public static void setPassword(String password) {
ApiAuthRestPost.password = password;
}
public static void setURLBase(String uRLBase) {
URLBase = uRLBase;
}
@Override
protected String doInBackground(String... params) {
HttpHost proxy = new HttpHost("puneproxy.igate.com", 8080);
String URL = URLBase + params[0];
String response = "";
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
try {
HttpGet httpGet = new HttpGet(URL);
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials(username, password);
BasicScheme scheme = new BasicScheme();
Header authorizationHeader = null;
try {
authorizationHeader =
scheme.authenticate(credentials, httpGet);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpGet.addHeader("Proxy- Authorization",
authorizationHeader.getValue());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
System.out.println("Executing request for get: " + httpGet.getRequestLine());
try {
response = httpclient.execute(httpGet,responseHandler);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("response : "+response);
} finally {
httpclient.getConnectionManager().shutdown();
}
return response;
}
}
My work place uses proxy. So I ve set the proxy in the above code. I have also made changes to the manifest file by adding the following line of code.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Also i vae added following line to the properties fie of project
manifestmerger.enabled=true
I am getting the following exception.
06-27 07:03:49.030: W/System.err(823):
java.util.concurrent.ExecutionException: java.lang.SecurityException:
Permission denied (missing INTERNET permission?)
06-27 07:03:49.030: W/System.err(823):at java.util.concurrent.FutureTask.report
(FutureTask.java:93)
06-27 07:03:49.030: W/System.err(823): at
java.util.concurrent.FutureTask.get(FutureTask.java:163)
06-27 07:03:49.040: W/System.err(823):at android.os.AsyncTask.get(AsyncTask.java:483)
06-27 07:03:49.040: W/System.err(823):at com.example.app.AddPerson$1.onClick
(AddPerson.java:56)
06-27 07:03:49.040: W/System.err(823):
at android.view.View.performClick(View.java:4438)
06-27 07:03:49.040:
W/System.err(823): at android.view.View$PerformClick.run(View.java:18422)
06-27 07:03:49.040: W/System.err(823):at android.os.Handler.handleCallback
(Handler.java:733)
06-27 07:03:49.040: W/System.err(823): at android.os.Handler.dispatchMessage
(Handler.java:95)
06-27 07:03:49.040: W/System.err(823):
at android.os.Looper.loop(Looper.java:136)
06-27 07:03:49.040:
W/System.err(823):at android.app.ActivityThread.main(ActivityThread.java:5017)
06-27 07:03:49.040:
W/System.err(823):at java.lang.reflect.Method.invokeNative(Native Method)
06-27 07:03:49.040
: W/System.err(823):at java.lang.reflect.Method.invoke(Method.java:515)
06-27 07:03:49.040:
W/System.err(823): at com.android.internal.os.
ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-27 07:03:49.040:
W/System.err(823): at com.android.internal.os.
ZygoteInit.main(ZygoteInit.java:595)
06-27 07:03:49.040:
W/System.err(823): at dalvik.system.NativeStart.main(Native Method)
06-27 07:03:49.040: W/System.err(823):
Caused by: java.lang.SecurityException:
Permission denied (missing INTERNET permission?)
06-27 07:03:49.070: W/System.err(823):
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
06-27 07:03:49.070: W/System.err(823):
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-27 07:03:49.070: W/System.err(823):
at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-27 07:03:49.080: W/System.err(823):
at org.apache.http.impl.conn.
DefaultClientConnectionOperator.openConnection
(DefaultClientConnectionOperator.java:137)
06-27 07:03:49.080: W/System.err(823):at org.apache.http.impl.conn.
AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-27 07:03:49.080:
W/System.err(823):at org.apache.http.impl.conn.
AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-27 07:03:49.090: W/System.err(823):
at org.apache.http.impl.client.
DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-27 07:03:49.090: W/System.err(823):
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-27 07:03:49.100: W/System.err(823):
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
06-27 07:03:49.100: W/System.err(823):
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
06-27 07:03:49.100: W/System.err(823):
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
06-27 07:03:49.110: W/System.err(823):
at com.example.app.ApiAuthRestGet.doInBackground(ApiAuthRestGet.java:96)
06-27 07:03:49.110: W/System.err(823):
at com.example.app.ApiAuthRestGet.doInBackground(ApiAuthRestGet.java:1)
06-27 07:03:49.110: W/System.err(823):
at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-27 07:03:49.120: W/System.err(823):
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-27 07:03:49.120: W/System.err(823):
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-27 07:03:49.120: W/System.err(823):
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-27 07:03:49.130: W/System.err(823):
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-27 07:03:49.130: W/System.err(823):
at java.lang.Thread.run(Thread.java:841)
06-27 07:03:49.130: W/System.err(823):
Caused by: libcore.io.GaiException:
getaddrinfo failed: EAI_NODATA (No address associated with hostname)
06-27 07:03:49.160: W/System.err(823):
at libcore.io.Posix.getaddrinfo(Native Method)
06-27 07:03:49.160: W/System.err(823):
at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
06-27 07:03:49.160: W/System.err(823):
at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
06-27 07:03:49.170: W/System.err(823): ... 18 more
06-27 07:03:49.180: W/System.err(823):
Caused by: libcore.io.ErrnoException:
getaddrinfo failed: EACCES (Permission denied)
I have not designed the web services my self. But I am using the web services of an online rest api. I cant use Retrofit to redesign web service. Please help me resolve this issue