안녕하세요, 안드로이드 응용 프로그램에서 웹 서버로 데이터를 보내고 mysql을 사용하여 데이터베이스로 보내려고합니다. 오류가없는 것 같습니다. 내 코드는 아닙니다. 작업json을 사용하여 안드로이드에서 데이터를 보내려면
안드로이드 자바 코드 :
String categorys=category.getText().toString();
String authors = author.getText().toString();
String quess = question.getText().toString();
String anss = answer.getText().toString();
try {
JSONObject json = new JSONObject();
json.put("category",categorys);
json.put("ques",quess);
json.put("ans",anss);
json.put("authors",authors);
postData(json);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
public void postData(JSONObject json) throws JSONException {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://shlomo.webuda.com/androidtomy.php");
List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
nvp.add(new BasicNameValuePair("json", json.toString()));
//httppost.setHeader("Content-type", "application/json");
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
if(response != null) {
InputStream is = response.getEntity().getContent();
//input stream is response that can be shown back on android
}
}
catch (Exception e)
{
e.printStackTrace();
}
}`
PHP 코드
mysql_connect("something","something","something");
mysql_select_db("something");
$json = $_SERVER['HTTP_JSON'];
echo "JSON: \n";
var_dump($json);
echo "\n\n";
$data = json_decode($json,true);
var_dump($data);
$category=$data['category'];
$author=$data['authors'];
$question=$data['ques'];
$answer=$data['ans'];
$sql = 'INSERT INTO Ques(Ques_Author, Ques_Question,Ques_Answer,Ques_Category,Ques_Approve) values("category","author","question","answer","0")';
mysql_query($sql);
}
>
정의 자바 코드에서 요청의 사본은 – njzk2
아무것도 데이터베이스에 간다 "작동하지 않습니다" 삽입 쿼리는 아무 것도 보내지 않습니다. –
값이'json'이고 POST 변수 일 때'$ _SERVER [ 'HTTP_JSON']'이 (가) 단순히 $ _POST [ 'json']'이 아니어야합니까? – njzk2