public class MainActivity extends AppCompatActivity {
EditText firstname, lastname, age;
Button insert, show;
RequestQueue requestQueue;
String insertUrl = "http://127.0.0.1/students/insertStudent.php";
String showUrl = "http://127.0.0.1/students/showStudents.php";
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstname = (EditText) findViewById(R.id.editText);
lastname = (EditText) findViewById(R.id.editText2);
age = (EditText) findViewById(R.id.editText3);
insert = (Button) findViewById(R.id.insert);
show = (Button) findViewById(R.id.showstudents);
result = (TextView) findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(getApplicationContext());
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("ww");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
showUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
try {
JSONArray students = response.getJSONArray("students");
for (int i = 0; i < students.length(); i++) {
JSONObject student = students.getJSONObject(i);
String firstname = student.getString("firstname");
String lastname = student.getString("lastname");
String age = student.getString("age");
result.append(firstname + " " + lastname + " " + age + " \n");
}
result.append("===\n");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.append(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
}
});
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> parameters = new HashMap<String, String>();
parameters.put("firstname",firstname.getText().toString());
parameters.put("lastname",lastname.getText().toString());
parameters.put("age",age.getText().toString());
return parameters;
}
};
requestQueue.add(request);
}
});
}
}
при нажатии на кнопу "insert student" вываливается
03-15 09:43:10.813 19189-19243/practise.mysql_php_json I/qtaguid: Tagging socket 42 with tag 59c3b57d00000000(1505998205) for uid -1 failed errno=-2
03-15 09:43:10.863 19189-19243/practise.mysql_php_json I/NetworkManagementSocketTagger: tagSocketFd(42, 1505998205, -1) failed with errno-2
03-15 09:43:10.872 19189-19243/practise.mysql_php_json I/qtaguid: Untagging socket 42 failed errno=-2
03-15 09:43:10.872 19189-19243/practise.mysql_php_json W/NetworkManagementSocketTagger: untagSocket(42) failed with errno -2
при нажатии на "showstudent" вываливается
03-15 09:44:18.243 19189-19189/practise.mysql_php_json I/System.out: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80) after 2500ms: isConnected failed: ECONNREFUSED (Connection refused)ww
03-15 09:44:18.263 19189-19244/practise.mysql_php_json I/qtaguid: Tagging socket 42 with tag 59c3b57d00000000(1505998205) for uid -1 failed errno=-2
03-15 09:44:18.263 19189-19244/practise.mysql_php_json I/NetworkManagementSocketTagger: tagSocketFd(42, 1505998205, -1) failed with errno-2
03-15 09:44:18.263 19189-19244/practise.mysql_php_json I/qtaguid: Untagging socket 42 failed errno=-2
03-15 09:44:18.263 19189-19244/practise.mysql_php_json W/NetworkManagementSocketTagger: untagSocket(42) failed with errno -2
IP
127.0.0.1 значит, что вы хотите приконнектиться к самому устройству, с которого коннектитесь. К самому себе. Если коннектитесь с телефона - 127.0.0.1 - это будет IP адресс самого телефона. Если с компьютера - адресс самого компьютера. А теперь подумайте, запущен ли на самом телефоне веб-сервер, накотором есть файлstudents/insertStudent.php
?