I'm really confused, I'm trying to develop a simple function that allows me to send and receive data from the server.
The operation is as follows:
In action, I perform an HTTP POST for the PHP file on the server, the โPHP fileโ receives the data that I send (typically a string), and executes the request using parameters sent via http.
Example:
My android app sends a string with this "PIPPO" value, there is a request in the PHP file, for example:
$ value = PIPPO / * data received from the android application * / Select * from the characters where (characters.name = ". $ Value.")
ps all data uses JSON format
The problem is this: I always used the function (this works fine), but now all the methods are outdated, I can not find alternatives to the latest API methods.
This is my code:
public class ReadServer extends Activity { String result; public String readserver(String id_data, String data){ try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("myurl/queryMobile.php"); StringBuilder builder = new StringBuilder(); String json = ""; //Build jsonObject JSONObject jsonObject = new JSONObject(); jsonObject.accumulate(id_data, data); //Convert JSONObject to JSON to String json = jsonObject.toString(); //Set json to StringEntity StringEntity se = new StringEntity(json); //Set httpPost Entity httpPost.setEntity(se); //Set some headers to inform server about the type of the content httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); //Execute POST request to the given URL HttpResponse httpResponse = httpclient.execute(httpPost); //Receive response as inputStream StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); //Convert input stream to string AlertDialog.Builder alertDialog; switch(statusCode){ case 200: HttpEntity entity = httpResponse.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line=""; try{ while ((line = reader.readLine()) != null) { builder.append(line); result = builder.toString(); } }catch(Exception e){ alertDialog = new AlertDialog.Builder(this); alertDialog.setTitle("400 Bad Request"); alertDialog.setMessage("Non รจ stato possibile soddisfare la tua richiesta, riprova piรน tardi."); alertDialog.show(); } break;
user4252099
source share