0
請幫忙。 我想使用okhttpclient從android插入數據到服務器(php),代碼不起作用。 請大家幫忙,我一直在那呆1天以上android okohttp插入數據到php不起作用
謝謝。
這裏是我的PHP代碼:
$con=mysqli_connect("localhost","root","","Androidtesting");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['email']))
{
//insert query
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "INSERT INTO users (email, password)
VALUES (".$email.", ".$password.");";
mysqli_query($con, $sql);
}
mysqli_close($con);
我的Android代碼:
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
String email = "myemail";
String password = "mypassword";
RequestBody body = new FormEncodingBuilder()
.add("contentType", "application/x-www-form-urlencoded")
.add("email", email)
.add("password", password)
.build();
Request request = new Request.Builder()
.url("http://10.0.2.2/AndroidTestingProject/data.php")
.post(body)
.build();
Response response = client.newCall(request).execute();
response.body().string();
if (!response.isSuccessful()) throw new
IOException("Unexpected code " + response);
}catch(Exception e)
{
System.out.println("FIL FUNCTION errroros hwa "+e);
}
}
});
thread.start();
請複製你的錯誤日誌。 – iBobb
請刪除'Please help's。當然這個論壇是爲了幫助你。現在不用了,謝謝。當你得到幫助時,你可以感謝你。 – greenapps
安卓工作室沒有錯誤@iBobb –