I have a problem with some deprecated methods, in particular with this that i posted here.
What that i want is simple: send data to server using http post ---> make a query with the data that i receive from a client --> send a reply
I'm trying to develop an Android application using API 6.0 but all my methods are deprecated, what I must change to convert my code with new ?
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;
case 500:
alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("500 Internal Server Error");
alertDialog.setMessage("Non è stato possibile soddisfare la tua richiesta, riprova più tardi.");
alertDialog.show();
break;
case 503:
alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("503 Service Unavailable");
alertDialog.setMessage("Il server di ....non è al momento disponibile, riprova più tardi.");
alertDialog.show();
break;
case 504:
alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("504 Gateway Timeout");
alertDialog.setMessage("Il server di ....è momentaneamente sovraccarico, riprova più tardi.");
alertDialog.show();
break;
default:
alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Si è verificato un errore");
alertDialog.setMessage("Errore 001" +"
"+"Non è stato possibile soddisfare la tua richiesta, riprova più tardi.");
alertDialog.show();
break;
}
}catch(Exception e){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Si è verificato un errore");
alertDialog.setMessage("Errore 001" + "
" + "Non è stato possibile soddisfare la tua richesta, riprova più tardi.");
alertDialog.show();
}
return result;
}
}
I tried to change some method but I don't know how to convert some procedure, for example :
setEntity, HttpResponse, StatusLine...
I absolutely need to change the deprecated method with new, i can't change all the code.
EDIT 1:
For example, in my MainActivity:
ReadServer read = new ReadServer();
String result = read.readserver("list_news","homepage");
My class ReadServer get in input two params:
public String readserver(String id_data, String data){
...
jsonObject.accumulate(id_data, data); // in a jsonObject i put two params
...
}
I make an Http Post, my data are send to my server (my php page)
$raw = file_get_contents('php://input');
$value = json_decode($raw, true);
$list_news = $value['list_news'];
At this point i get my data and i can make my query:
if (isset($list_news)) {
switch($list_news){
case "homepage":
$q = "SELECT
FROM
WHERE ";
See Question&Answers more detail:
os