Во-первых, в вашем php-файле попробуйте использовать
die(var_dump($_POST))
, чтобы проверить, правильно ли ваше почтовое приложение!
Это код моей активности:
@Override
protected List<MyModel> doInBackground(Void... params) {
Logger.log(Logger.INFO, "DOWNLOAD STARTED!");
try {
//I have a Service Class called ConnectionService, that do the verifications and conenction!
if (connectionService.isConnected && connectionService.hasInternet) {
//Creating my json to send via POST to my wbs in php.
JSONObject data = new JSONObject();
data.put("my_attribute", "AttributeTest");
JSONObject newData = new JSONObject().accumulate("data", data);
return connectionService.downloadInfo(newData);
}
return null;
} catch (Exception e) {
Message.showLongMessage(getApplicationContext(), getString(R.string.error_no_connection));
Logger.log(Logger.ERROR, e.getMessage());
}
return null;
}
Это мой класс ConnectionService:
public List downloadInfo(JSONObject dataJson) {
try {
return checkDownloadConnection(connectPost(dataJson));
} catch (IOException | JSONException e) {
e.printStackTrace();
Logger.log(Logger.ERROR, e.getMessage());
}
return null;
}
private JSONObject connectPost(JSONObject dataJson) {
JSONObject jsonObject = null;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL("my_url_to_my_php_file").openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
//connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod("POST");
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
OutputStream os = connection.getOutputStream();
os.write(dataJson.toString().getBytes("UTF-8"));
os.flush();
if (connection.getResponseCode() == 200) {
jsonObject = new JSONObject(Utils.bytesToString(connection.getInputStream()));
}
} catch (IOException | JSONException e) {
e.printStackTrace();
Logger.log(Logger.ERROR, e.getMessage());
}
if (connection != null) {
connection.disconnect();
}
return jsonObject;
}
public List checkDownloadConnection(JSONObject connectionJson) throws JSONException, IOException {
if (connection == null) {
return null;
}
//connectionJson will return a result from my php json_encode(die(var_dump($obj)))
//with status and data "$result = ['status' => true/false, 'message' => 'my message if occur some error', 'data' => $myData];
String status = String.valueOf(connectionJson.getString("status"));
if (status.equals(STATUS_FALSE)) {
Logger.log(Logger.ERROR, String.valueOf(connectionJson.getString("message")));
return null;
}
return hydrateObjects(connectionJson.getJSONArray("data"));
//this is another function, that i hydrate my info downloaded, you won't need it for your logic.
}
Моя функция Utils.bytesToString:
public static String bytesToString(InputStream is) throws IOException {
byte[] buf = new byte[1024];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
buffer.write(buf, 0, bytesRead);
}
String out = new String(buffer.toByteArray());
return out;
}
Надеюсь, вы понимаете.:)