Android JSON for PHP Server and vice versa

Can anyone suggest a solution above?

For now, all I want to do is send a JSON request to my server (for example: {picture: jpg, color: green}), access the PHP database, and then return the file name from the server database (then run android file to upload file is not a problem)

Can anyone suggest an Android Framework first that will help me with this. - POST JSON to php file on my server

And secondly, a php script that will turn JSON into readable in php format (accessing the database is also not a problem, but I cannot get JSON into an object so that it matches the database)

thanks

EDIT

Thanks for the links below, but I'm not asking about it out of laziness, it's because of the annoyance at my seeming inability to send a JSON string and getting the right answer.

So let me show the code and find out why what I think should happen doesn't happen:

GET URL (using GET so I can show the work)

http://example.com/process/json.php?service=GOOGLE <?php // decode JSON string to PHP object $decoded = json_decode($_GET['json']); // I'm not sure of which of the below should work but i've tried both. $myService = $decoded->service; //$service should equal: GOOGLE $myService = $decoded->{'service'}; //$service should equal: GOOGLE // but the result is always $myService = null - why? ?> 
+4
source share
2 answers

OK, I have PHP. The following returns the POST data and returns the service

 <?php $data = file_get_contents('php://input'); $json = json_decode($data); $service = $json->{'service'}; print $service; ?> 

and Android code:

in onCreate ()

 path = "http://example.com/process/json.php"; HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout // Limit HttpResponse response; JSONObject json = new JSONObject(); try { HttpPost post = new HttpPost(path); json.put("service", "GOOGLE"); Log.i("jason Object", json.toString()); post.setHeader("json", json.toString()); StringEntity se = new StringEntity(json.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); response = client.execute(post); /* Checking response */ if (response != null) { InputStream in = response.getEntity().getContent(); // Get the // data in // the // entity String a = convertStreamToString(in); Log.i("Read from Server", a); } } catch (Exception e) { e.printStackTrace(); } 

and where do you want

 private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } 
+14
source

On the PHP side, all you need is a built-in json_decode that will deserialize your json and return an object (or an associative array if you pass true as the second argument).

On the Android side, you are likely to use HTTP libraries to fulfill your HTTP request and process the response. (But someone who really developed for Android can fix me)

+2
source

Source: https://habr.com/ru/post/1335470/


All Articles