我正在尝试使用Android应用在mysql数据库的表PERSON(nom,pass_world)中添加一行。
当我在浏览器中执行php代码(savedata.php)时,它显示了我:query_result“:” SUCCESS“,这意味着已成功添加一行。
我在Google中搜索了一些代码,该代码允许android应用将数据添加到mysql数据库中,然后我尝试了一下,但不幸的是,它抛出JSONEexcption并显示消息“解析JSON数据时出错”,如下面的代码所示:
savedata.php
<?php
$con=mysqli_connect("localhost","root","","othmane");
if (mysqli_connect_errno($con))
{
echo '{"query_result":"ERROR"}';
}
$name = $_GET['nom'];
$pass = $_GET['pass_world'];
$result = mysqli_query($con,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");
if($result == true) {
echo '{"query_result":"SUCCESS"}';
}
else{
echo '{"query_result":"FAILURE"}';
}
mysqli_close($con);
?>
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText inputName;
EditText inputpwd;
Button button;
String nom, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputName = (EditText) (findViewById(R.id.editText));
inputpwd = (EditText) (findViewById(R.id.editText2));
button = (Button) findViewById(R.id.button);
}
public void save(View view) {
nom = inputName.getText().toString();
password = inputpwd.getText().toString();
new SignupActivity(this).execute(nom, password);
}
}
SignupAtivity.java
public class SignupActivity extends AsyncTask<String, Void, String> {
private Context context;
public SignupActivity(Context context) {
this.context = context;
}
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
String link;
String data;
BufferedReader bufferedReader;
String result;
try {
data = "?nom=jean";
data += "&pass_world=123456";
link = "http://localhost/save/savedata.php" + data;
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
result = bufferedReader.readLine();
return result;
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
String jsonStr = result;
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String query_result = jsonObj.getString("query_result");
if (query_result.equals("SUCCESS")) {
Toast.makeText(context, "Data inserted successfully. Signup successfull.", Toast.LENGTH_SHORT).show();
} else if (query_result.equals("FAILURE")) {
Toast.makeText(context, "Data could not be inserted. Signup failed.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context,"Error parsing JSON data.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
}
}
}
日志猫:
03-07 06:42:26.992 8503-8503 / com.example.othmane.myapplication D / !!:例外:无法连接到localhost / 127.0.0.1(端口80):连接失败:ECONNREFUSED(连接被拒绝)
03-07 06:42:29.844 8503-8503 / com.example.othmane.myapplication W / System.err:org.json.JSONException:java.lang.String类型的值异常无法转换为JSONObject
03-07 06:42:29.845 8503-8503 / com.example.othmane.myapplication W / System.err:位于org.json.JSON.typeMismatch(JSON.java:111)
03-07 06:42:29.845 8503-8503 / com.example.othmane.myapplication W / System.err:位于org.json.JSONObject。(JSONObject.java:160)
03-07 06:42:29.845 8503-8503 / com.example.othmane.myapplication W / System.err:位于org.json.JSONObject。(JSONObject.java:173)
03-07 06:42:29.845 8503-8503 / com.example.othmane.myapplication W / System.err:位于com.example.othmane.myapplication.SignupActivity.onPostExecute(SignupActivity.java:60)
03-07 06:42:29.845 8503-8503 / com.example.othmane.myapplication W / System.err:位于com.example.othmane.myapplication.SignupActivity.onPostExecute(SignupActivity.java:20)
03-07 06:42:29.845 8503-8503 / com.example.othmane.myapplication W / System.err:位于android.os.AsyncTask.finish(AsyncTask.java:651)
03-07 06:42:29.845 8503-8503 / com.example.othmane.myapplication W / System.err:位于android.os.AsyncTask.-wrap1(AsyncTask.java)
03-07 06:42:29.845 8503-8503 / com.example.othmane.myapplication W / System.err:at android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:668)
03-07 06:42:29.846 8503-8503 / com.example.othmane.myapplication W / System.err:位于android.os.Handler.dispatchMessage(Handler.java:102)
03-07 06:42:29.847 8503-8503 / com.example.othmane.myapplication W / System.err:at android.os.Looper.loop(Looper.java:148)
03-07 06:42:29.847 8503-8503 / com.example.othmane.myapplication W / System.err:位于android.app.ActivityThread.main(ActivityThread.java:5417)
03-07 06:42:29.847 8503-8503 / com.example.othmane.myapplication W / System.err:at java.lang.reflect.Method.invoke(本机方法)
03-07 06:42:29.847 8503-8503 / com.example.othmane.myapplication W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
03-07 06:42:29.847 8503-8503 / com.example.othmane.myapplication W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-07 06:42:29.847 8503-8503 / com.example.othmane.myapplication D / !!:例外:无法连接到localhost / 127.0.0.1(端口80):连接失败:ECONNREFUSED(连接被拒绝)
最佳答案
您必须提及提供POST服务的服务器的IP地址。
link = "http://localhost/save/savedata.php" + data;
使成为
link = "http://<IP ADDRESS : PORT>/save/savedata.php" + data;