android: how to retrieve data from sql server using web service - android

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.

Related

Provide authorization in Rest Web service in the android client

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

Sending POST json from Android and receiving on Django

I am trying to get an android app to interact with a server in Django.
The app is trying to POST "json" data to Django. However, I am unable to receive the object on the Django end.
The value of request.POST is <QueryDict: {}> although the data sent isn't blank. Following is the code snippet for POST request from android.
public static String POST(String url,JSONObject obj){
InputStream inputStream = null;
String result = "";
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = obj.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type","application/json");
HttpResponse httpResponse = httpClient.execute((HttpUriRequest)httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream!=null){
result = convertInputStreamToString(inputStream);
}else{
result = "Did not work!";
}
}catch(Exception e){
}
return result;
}
EDIT:
Earlier, I was getting CSRF error and handled it this way (I haven't worked with Django enough to know if this is correct way to handle CSRF error)
#csrf_exempt
def search(request):
logger.debug(request.POST)
"""Code for JSON object processing"""
Any help with rectifying the problem would be highly appreciated.
OK I'm not very fluent in java but it seems to me that your request is well formed.
I think the issue is that you are sending the data as a json string instead of as if it was a raw form. When you do it this way, the data is not displayed in request.POST but in request.body as what it is: a json string, not form-like data.
So I think you have to take one of these ways:
send the data from the Android app as a form (not json-like). This way you'll see it in request.POST or
translate request.body into a dict and work with it instead of request.POST
Hope this helps! :)

Using webservices in android

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

"SSLPeerUnverifiedException: no peer certificate" android backend

so this is for a project im doing for my school in which we need a backend.
our current setup is that the foreground is an android app, whereas the backend is a mySQL database that is located within my school's servers. The android app is supposed to interact with the mySQL database using a php script.
my php script is set currently hardcoded to return one single row from the mySQL database in JSON format.
my android code is as follows:
public static final String KEY = "URL of my PHP script";
InputStream is = null;
String result = "";
try{
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(KEY));
Log.d(null,"there");
HttpResponse response = httpclient.execute(request);
Log.d(null, "here");
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
However, I get an exception at the line
HttpResponse response = httpclient.execute(request);
in which the error says: No Peer Certificate.
Anyone know what's wrong with it, and how to fix it?
I have the same issue. This link worked for part of my needs.
http://www.knowledgebit.appspot.com/zahangirbd/TopicView.action;jsessionid=E2BZt_6bp4uFFbMyq42gWg?id=56001
however, there is still an issue with some links(i believe im not providing the proper certificate)
EDIT: so it turns out that the order of the certificates were the problem.
check this out, this helped me..
Apache HttpClient on Android producing CertPathValidatorException (IssuerName != SubjectName)

android server post data

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

Categories

Resources