Sending data from Android App to PHP with JSON is not working

Saturday, October 17, 2015

We are making a project voor School and we have some problems with sending data from our Android App to our PHP-controller.

First of all :

here is the code :
First of all , almost the full Code of our AddScoreActivity

  public static String POST(String url,ScoreData score){      InputStream inputStream = null;      String result = "";      try{          HttpClient httpClient = new DefaultHttpClient();          HttpPost httpPost = new HttpPost(url);          String json = "";            JSONObject jsonObject = new JSONObject();          jsonObject.accumulate("scoreTotaal", score.getTotaleScore());          jsonObject.accumulate("scoreStrikes",score.getAantalStrikes());          jsonObject.accumulate("scoreSpares",score.getAantalSpares());          jsonObject.accumulate("Game_ID",3);          jsonObject.accumulate("Google_ID",3);            json = jsonObject.toString();          StringEntity se = new StringEntity(json);          httpPost.setEntity(se);            httpPost.setHeader("Accept","application/json");          httpPost.setHeader("Content-type","application/json");            HttpResponse httpResponse = httpClient.execute(httpPost);          inputStream = httpResponse.getEntity().getContent();            if(inputStream != null)          {              result = convertInputStreamToString(inputStream);          }          else          {              result = "Did not work!";          }      }      catch(Exception e)      {          Log.d("Inputstream", e.getLocalizedMessage());        }        return result;    }    

@Override
public void onClick(View view) {

      TotaleScore = editTotaleScore.getText().toString();      AantalStrikes = editAantalStrikes.getText().toString();      AantalSpares = editAantalspares.getText().toString();        switch(view.getId()){          case R.id.submitScoreButton:              if(!validate())                  Toast.makeText(getBaseContext(),"Enter some data!",Toast.LENGTH_LONG).show();                  new HttpAsyncTask().execute("http://localhost/ICTProjects3/ScoreController");              break;      }    }        private class HttpAsyncTask extends AsyncTask<String, Void, String> {      @Override      protected String doInBackground(String... urls) {            score = new ScoreData();            score.setTotaleScore(TotaleScore);          score.setAantalStrikes(AantalStrikes);          score.setAantalSpares(AantalSpares);            return POST(urls[0],score);      }     private boolean validate(){      if(editTotaleScore.getText().toString().trim().equals(""))          return false;      else if(editAantalStrikes.getText().toString().trim().equals(""))          return false;      else if(editAantalspares.getText().toString().trim().equals(""))          return false;      else          return true;  }    private static String convertInputStreamToString(InputStream inputStream) throws IOException{      BufferedReader bufferedReader = new BufferedReader( new InputStreamReader((inputStream)));      String line = "";      String result = "" ;      while ((line = bufferedReader.readLine()) != null)          result += line;        inputStream.close();;      return result;  }    

I think this is working well. If i'm using my Android Emulator & looking to fiddler. My emulator is sending this to my PHP-controller :

  POST http://localhost/ICTProjects3/ScoreController HTTP/1.1 Accept:    application/json Content-type: application/json Content-Length: 85    Host: localhost Connection: Keep-Alive User-Agent:    Apache-HttpClient/UNAVAILABLE (java 1.4)     {"scoreTotaal":"5","scoreStrikes":"55","scoreSpares":"555","Game_ID":3,"Google_ID":3}    

Problem is. Its doing nothing. I can't add it to my database with my Controller. Its not a problem with my methode : ScoreToevoegen($aScoreData). Thats for putting it into my database. Its working webbased so normally it would work here.
This is my PHP-controller:

  if (isset($_POST['json']))      {          $aJson = $_POST['json'];          $aScoreData = json_decode($aJson, true);            $this->load->model('Score_model');          $this->Score_model->ScoreToevoegen($aScoreData); // Inserting it in the database.        }    

I really hope you guys can help me. I'm stuck on this moment.
Thank you!


Filed under: php

0 comments:

Post a Comment