1

I saw that the version of Android API 22 DefaultHttpClient is deprecated. In my old code I had to send data to a PHP page and get the response that the page php returned. By using this code:

HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("php page"); 

ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", "user"));
parameters.add(new BasicNameValuePair("password", "pw"));

HttpResponse response = client.execute(request);
String responseMessage = response.getEntity().toString();

I saw that NameValuePair is deprecated. My question is how can I do the same thing in another way not deprecated. As I said I need to send data (via post) to a php page and get what page PhP printing. (In my case a string JSON). Many thanks in advance!!.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • No thanks. The last months many others posted the same problem. So if you google a bit you find many answers on this site. – greenapps Oct 30 '15 at 19:38
  • Ma no grazie cosa?... che ti venissero le emorroidi al culo sfigato. Piuttosto che dare una risposta del genere potevi spararti una sega. – federicostudio96 Oct 30 '15 at 22:52
  • Possible duplicate of [Why I am getting DefaultHttpClient is deprecated?](http://stackoverflow.com/questions/24788521/why-i-am-getting-defaulthttpclient-is-deprecated) – e4c5 Oct 31 '15 at 06:56

1 Answers1

2

Use HttpURLConnection. Set you values as JSONObject instead of setting it as NameValuePair.

public HttpURLConnection createConnection(){

    try{            
        urlconnection=(HttpURLConnection)url.openConnection();      
    }catch(Exception e){
        Log.e("Can't create connections", e.getMessage());

    }

    return urlconnection;

}
public String doFunctionPost(JSONObject object){
    try{
        urlconnection=createConnection();
        urlconnection.setDoInput(true);
        urlconnection.setDoOutput(true);
        urlconnection.setRequestMethod(method);
        urlconnection.setUseCaches(false);
        urlconnection.setConnectTimeout(10000);
        urlconnection.setReadTimeout(10000);
        urlconnection.setRequestProperty("json", object.toString());
        urlconnection.setRequestProperty("Content-Type", "application/json");

        OutputStreamWriter out=new OutputStreamWriter(urlconnection.getOutputStream());
        out.write(object.toString());
           System.out.println(object.toString());
            out.close();
            httpResult=urlconnection.getResponseCode();
            System.out.println("Response Code"+ httpResult);
            result=readResponse();

    }catch(Exception e){
        Log.e("Response Error", e.getMessage());

    }finally{
        removeConnection();
    }
    return result;
}

public String readResponse(){
    try{
        if(httpResult==HttpURLConnection.HTTP_OK){
            BufferedReader buffer_reader=new BufferedReader(new InputStreamReader(urlconnection.getInputStream(),"utf-8"));
            String line=null;
            sb=new StringBuilder();
            while((line=buffer_reader.readLine())!=null){
                sb.append(line+"\n");
            }
            buffer_reader.close();
            System.out.println("buffer_reader"+sb.toString());
        }else{
            Log.e("Error on posting", urlconnection.getResponseMessage());
        }
    }catch(Exception e){
        Log.e("Error in Response", e.getMessage());
    }
    return sb.toString();
}