我们正在做一个项目,声音学校,我们有一些问题从我们的Android应用程序发送数据到我们的PHP控制器。
首先:
下面是代码:首先,几乎是我们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("Totaal", score.getTotaleScore());
jsonObject.accumulate("Strikes",score.getAantalStrikes());
jsonObject.accumulate("Spare",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 void onClick(视图){
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;
}我觉得这个效果很好。如果我使用的是我的Android仿真器&看手推车。我的模拟器将这个发送给我的PHP控制器:
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}问题是。它什么都不做。我不能用我的控制器把它添加到我的数据库中。这不是我的方法的问题: ScoreToevoegen($aScoreData)。这是用来把它放到我的数据库里的。它的工作网络基础上,所以它通常会在这里工作。这是我的PHP控制器:
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.
}我真的希望你们能帮我。我被困在这一刻了。谢谢!
编辑: ScoreToevoegen-Method &也是基于the的控制器(这是有效的!)它是用CodeIgniter做的
ScoreController.PHP
if(isset($_POST['UploadScore'])){
if ($this->form_validation->run() == FALSE) {
$this->load->view('ScoreView', $data);
}
else {
$aScoreData =[
//'Game_ID' => $this->input->post('gameID'),
//'Google_ID' => $this->input->post('googleID'),
'Game_ID' =>2,
'Google_ID' =>2,
'Totaal' => $this->input->post('scoreTotaal'),
'Strikes' => $this->input->post('scoreSpares'),
'Spare' => $this->input->post('scoreStrikes')
];
$this->load->model('Score_model');
$this->Score_model->ScoreToevoegen($aScoreData);
$this->load->view('ScoreView', $data);
}}Score_Model.PHP
public function ScoreToevoegen($aData)
{
$this->load->database();
$this->db->insert('score', $aData);
}发布于 2015-10-17 19:48:50
您要将JSON数据作为请求体发送,$_POST‘fetching’用于从已发布的表单中获取数据。
而不是这样:
if (isset($_POST['json']))你应该用这样的方法:
$aScoreData = json_decode(file_get_contents('php://input'), true);
if ($aScoreData) {
$this->load->model('Score_model');
$this->Score_model->ScoreToevoegen($aScoreData);
}https://stackoverflow.com/questions/33188015
复制相似问题