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?
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?
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 .
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.
Source: https://ru.stackoverflow.com/questions/623393/
All Articles