I am accessing a REST web service via Mozilla client via Mozilla browser. (In web service
back end spring security has configured and credentials for each
request is validated). So I am setting basic authentication (username and password) and
Request header "application/json" which is fine.
Now I want to connect to this web service via a android application as below. How do I set
credentials of the user (username and password) to the request ?
#Override
protected Void doInBackground(Void... params) {
HttpClient client = new DefaultHttpClient();
WsUrl="http://192.168.0.15:8080/ServiceApp/categoryservice/category/001"; // RESTFUL URL RESOURCE
HttpGet getRequest = new HttpGet(WsUrl);
try {
HttpResponse response=client.execute(getRequest);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode != 200){
return null;
}
InputStream jsonStream = response.getEntity().getContent();
ufferedReader reader = new BufferedReader(new InputStreamReader(jsonStream));
check the link for answer, I don't what kind of authentication you are using, but this link shows how to windows basic authentications
HTTP requests with basic authentication
Related
How to save and retrieve data from database. Can somebody give me a little help how to connect web service with database and in app, what would be the first step to start. Thanks.
Build a web server, which will be sending the JSon objects to you
Then use http calls to get this Json, like this:
public String httpGet(String url) throws IOException{
HttpGet get = new HttpGet(url);
DefaultHttpClient() _httpClient = new DefaultHttpClient();
BasicHttpContext httpContext = new BasicHttpContext();
HttpResponse response=null;
response = httpClient .execute(get, httpContext);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
return EntityUtils.toString(response.getEntity());
}
For more information, leave me a message, please.
To communicate with a remote database: database on server for example you need to communicate with an API. They are several ways to build an API: PHP, ruby.
Communicating with a remote db is forbidden in most of hosts for security purpose.
I am sending information to a server via WIFI and everything works great.Now i want to send information to a server with mobile data too, and i do not know why only works with WIFI, with mobile data trows an exception of failed to connect to server.
this is the part that fail with mobile data; with WIFI works perfectly:
int length=values.length();
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,9000);
HttpConnectionParams.setSoTimeout(httpParams, 9000);
HttpClient client = new DefaultHttpClient(httpParams);
String url = saveData+"?Length="+length+"&Table="+temp;
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(values.toString().getBytes("UTF8")));
request.setHeader("json", values.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
Log.d("test 7","test 7 last");
if (entity != null) {
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream);
Log.d("here",""+result);
if(result.equals("success")&& ReadyOff==false){
Ready=true;
}else{
Ready=false;
ReadyOff=true;
}
Log.d("sent","valor de ready"+Ready);
}
so i am doing something wrong?
`
You cannot contact your server from the device's mobile network unless it is routable from the public Internet.
A server running on your development machine or otherwise behind a NAT/firewall would typically only be accessible from your local network / wifi.
I am developing a Android app, which communicates with a RESTful WCF Web Service in my server.
By using HttpClient, the application can read the json code from a url link.
For Example:
http://www.example.com/WebService/Service.svc/subscriptions/tiganeus
returns {"JSONUserDataResult":["Application A","Application B"]}
However, this web service itself is anonymous accessible, but protected by ISA Server.
Browser automatically shows "Authentication Required" dialog when this link is accessed externally. Simply fill in the username and password is OK.
I figured out how to do authentication in a webview. The following code works
private class MyWebViewClient extends WebViewClient {
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
handler.proceed("username", "password");
System.out.println("httpa");
}
}
But what I really need is to read the JSON code from the url.
I have chosen to use HttpClient to do the job, but I can't figure out how to authenicate within the HttpClient. It sounds simple as any browser can do it.
Any help will be appreciated.
Apparently it is much simpler than I thought.
DefaultHttpClient() httpClient = new DefaultHttpClient();
httpClient.getCredentialsProvider().setCredentials(
AuthScope.ANY,
new NTCredentials(user, pass, "tamtam", "tamtam"));
URI uri = new URI("http://www.example.com/WebService/Service.svc/subscriptions/tiganeus");
HttpGet httpget = new HttpGet(uri);
httpget.setHeader("Content-type", "application/json; charset=utf-8");*/
HttpResponse response = httpClient.execute(httpget);
HttpEntity responseEntity = response.getEntity();
String result = EntityUtils.toString(responseEntity);
httpClient.getCredentialsProvider().setCredentials works fine for the authentification through isa server
(at least for in the way my isa-server is configured.
It handles basic authentication as well.
I have the following site http://www.freewebservicesx.com/GoldSpotPrice.aspx which provides a webservice api. As i am extremely new to soap and rest i have absolutely no clue about how to call this service and use it in android. Could someone please tell me how to do it.
You can make HTTP requests using either HttpURLConnection or HttpClient. Example of using HttpClient for a RESTful web service:
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.mywebsite.com/webservice");
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
out.close();
String responseStr = out.toString();
// process response
} else {
// handle bad response
}
Otherwise, if you're working with a SOAP web service, I would recommend using ksoap2
Use the following url to refer:::
Using ksoap2 for android, and parsing output data...
ksoap2-android - HowToUse.wiki.....
ksoap2-android
Use this LINK it has a sample the suits your requirement.
also LINK
I am new to android networking and would like to find the best solution/source that would help me learn the same. I have a working android application but i want to include network services that can get and send data to the webserver. While i was searching for the same i found link ( http://www.helloandroid.com/tutorials/connecting-mysql-database ) which dont produce any results. I also found that SOAP or REST (with android) are probably recommended methods, if so please give complete tutorials to learn the same ( i have no prior knowledge on webservices). In my application I would be required to send data to the server and receive data from the servers sql
Thank you
This is for post data on server,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// get response entity
HttpEntity entity = response.getEntity();
// convert entity response to string
if (entity != null)
{
InputStream is = entity.getContent();
// convert stream to string
result = convertStreamToString(is);
result = result.replace("\n", "");
}
or see how to post data to remote server in android app [closed], Post the data to Server