Send and receive data from server using 6.0 API (Android)
<p>I'm really confused, i'm trying to develop a simple function that allow me to send and receive data from a server.</p>

<p>The operation is as follows:</p>

<p>In an activity I execute an HTTP POST to a PHP file on a server, the "PHP file" gets the data that i send (tipically a string), and executes a query using the parameters sent over http.</p>

<p>Example:</p>

<p>My android app send a string with this value "PIPPO", in the PHP file  there is a query, for example:</p>

<p>$value = PIPPO /* data received from android app*/
Select * from characters where(characters.name=".$value.")</p>

<p>p.s. all data use JSON format</p>

<p>The problem is:
i always used a function (that works fine) but now all methods are deprecated, I can't find an alternative to the methods for the newest API.</p>

<p>This is my code:</p>

<pre><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;
</code></pre>