0

Problem on my part is solved The owner of the server removed some code to check if the basics of sending a POST/GET method works and that works perfectly.

I'm still dont know what the problem is, but it sure is not my problem lol


I'm trying to send POST method to the server, but it turns out the server gets a GET method instead. We got it working on the iphone so the server is correct.

This is the code I written to send data:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://myurl.nl/casus");
    httppost.setHeader("Authorization", "xxxxxxxxxxxxxxx");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        //casus
        nameValuePairs.add(new BasicNameValuePair("casusid", casus.id+""));

        String woorden = "[";
        for(int i = 0; i < words.length; i++){
            woorden += "\""+words[i]+"\"";
            if(i != words.length-1){
                woorden += ",";
            }
        }
        woorden += "]";
        nameValuePairs.add(new BasicNameValuePair("woorden", woorden));
        //info
        SharedPreferences sharedPref = getSharedPreferences("omapp", MODE_PRIVATE);
        String age = sharedPref.getString("age", "");
        String sex = sharedPref.getString("sex", "");
        nameValuePairs.add(new BasicNameValuePair("leeftijdscategorie", age));
        nameValuePairs.add(new BasicNameValuePair("geslacht", sex));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String responseBody = EntityUtils.toString(response.getEntity());
        Log.d("json", responseBody);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

PHP code:

    // Service token authorization.
    require_once('../classlib/Auth.php');
    $auth = new Auth;
    if ( !$auth->valid() ) {
        header('HTTP/1.1 401 Unauthorized');
        exit;
    }

    // Setup casussen.
    require_once('../classlib/PDOFactory.php');
    require_once('../classlib/Casussen.php');
    $casussen = new Casussen;
    $casussen->setPDO(PDOFactory::create());

    $rs = (object) array();
    switch ( $_SERVER['REQUEST_METHOD'] ) {

        case 'GET': {
            mail('[email protected]', 'The get method was called', print_r($_SERVER,1));
            $fromcasusid = !empty($_GET['fromcasusid']) ? (int) $_GET['fromcasusid'] : null;
            $rs->casussen =$casussen->get($fromcasusid);
            break;
        }

        case 'POST': {
            mail('[email protected]', 'The post method was called', print_r($_SERVER,1));
            $casusreactie = json_decode(file_get_contents("php://input"));
            try {
                $casussen->post($casusreactie);
            } catch ( Exception $e ) {
                $rs->error = (object) array(
                    'code' => $e->getCode(),
                    'message' => $e->getMessage(),
                );
            }
            break;
        }

        default: {
            header('HTTP/1.1 405 Method Not Allowed');
            exit;
        }

    } // switch

    header('Content-type: application/json; charset=utf-8');
    exit(json_encode($rs));
8
  • as far as I see, your code looks correct. maybe there is a problem on the server? Mar 29, 2013 at 9:46
  • @Jaytjuh: Can you paste your server side code?
    – karthick
    Mar 29, 2013 at 9:48
  • I dont think so cause on the iphone when sending a POST method, the server does receive a POST method.
    – Jaytjuh
    Mar 29, 2013 at 9:53
  • got the php code :) Ill edit my post
    – Jaytjuh
    Mar 29, 2013 at 10:06
  • is there any redirection rule somewhere in the middle (i.e. in your web server configuration ? it is possible that this rule, if it exists don't apply to request comming from iPhone - because a guard based on useragent for instance)
    – ben75
    Mar 29, 2013 at 10:18

2 Answers 2

0

One workout..

call doPost method from inside your doGet

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{

doPOst(request, response)
}
1
  • he didnt know cause the php code was later added to my question
    – Jaytjuh
    Mar 29, 2013 at 10:39
0

I was also having a similar issues. I was able to fix it by add "www." to the url. But i would like to know how that is making a difference, I had successfully tested the server handling with a jQuery ajax request without the "www." .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.