Good afternoon, tell me how to create a JSON file and send it to the http server via POST.

JSONObject bot = new JSONObject(); try { instabot.put("Login", Login); instabot.put("Password", Password); } 

how to save to json file?

Closed due to the fact that the essence of the question is incomprehensible to the participants by Yuriy SPb , Denis Bubnov , user194374, tse , Akina 7 Feb '17 at 10:04 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    You do not need to create a json file to transfer it to the server. Just send it as a regular line. You can turn the JSONObject string to the toString method.

    Here is the simplest example:

     public class MainActivity extends Activity { ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new RequestTask().execute("http://www.site.ru/login.php"); // скрипт, на который посылаем запрос } public String getJSON(String login, String pass) // получаем json объект в виде строки { JSONObject bot = new JSONObject(); try { bot.put("Login", login); bot.put("Password", pass); } catch (JSONException e) { e.printStackTrace(); } return bot.toString(); } public class RequestTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { try { DefaultHttpClient hc = new DefaultHttpClient(); ResponseHandler<String> res = new BasicResponseHandler(); HttpPost postMethod = new HttpPost(params[0]); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); // ключ - "json", параметр - json в виде строки nameValuePairs.add(new BasicNameValuePair("json", getJSON("userlogin", "userpass"))); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8"); postMethod.setEntity(entity); return hc.execute(postMethod, res); } catch (Exception e) { System.out.println("Exp=" + e); } return null; } @Override protected void onPostExecute(String res) { dialog.dismiss(); // res - ответ сервера super.onPostExecute(res); } @Override protected void onPreExecute() { dialog = new ProgressDialog(MainActivity.this); dialog.setMessage("Ожидание"); dialog.setIndeterminate(true); dialog.setCancelable(false); dialog.show(); super.onPreExecute(); } } } 

    You can process the request, for example, in PHP:

     <?php $json = $_POST['json']; // получаем json объект $array = json_decode($json, true); // преобразуем его в ассоциативный массив // получаем из него данные $login = $array['Login']; $pass = $array['Password']; // дальше уже делаем то, что нужно file_put_contents('login.txt', "$login\n$pass"); ?> 

    To send a POST request, you can, for example, use the Retrofit 2 library. There are many examples of working with it on the Internet.

    • If there is a thread, please skip an example. - Alexey Smirnov
    • Added an example. - Kirill Malyshev
    • And what library should I put to make it all work? Retrofit? I'm trying to install apache lib ... bye bye) - Alexey Smirnov
    • This code requires HttpClient: stackoverflow.com/questions/32153318/… . But still, it is preferable to use more modern libraries, and I cannot tell you about them, unfortunately. - Kirill Malyshev