Skip to main content

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?




Comments

  1. Im using my PHP-Webservice this way:

    Java:

    // 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);

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?