Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Friday, January 20, 2012
How can I connect android to a php web service
can anybody guide me to a tutorial or a working example of connecting android to a php web service using ksoap2 or something else?
Im using my PHP-Webservice this way:
ReplyDeleteJava:
// params => "?action=getInfos"
private JSONObject getJSONObject(String params)
{
try
{
String url = path2webservicephp + params;
System.out.println("Call URL: " + url);
HttpGet request = new HttpGet(url);
request.setHeader("Content-Type", "text/xml;charset=UTF-8");
DefaultHttpClient httpclient2 = new DefaultHttpClient();
HttpResponse response = httpclient2.execute(request);
if (response.getStatusLine().getStatusCode() != 200)
throw new ConnectionException("Status Code is " + response.getStatusLine().getStatusCode() + ": " + response.getStatusLine().getReasonPhrase());
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String line = reader.readLine();
MyConstants.RECEIVEDBYTES += line.length();
reader.close();
if ("[]".equals(line))
return null;
return new JSONObject(line);
}
catch (Exception e)
{
LLog.e(Webservice.class, "Exception! params: " + params);
LLog.e(e, Webservice.class);
}
return null;
}
// LLog.e is from my own class. It logs to Log.e and into a file
PHP:
$array = array('status' => 'ok', 'message' => 'your action was: '.$_GET['action']);
echo json_encode($array);