federicostudio96
federicostudio96

Reputation: 61

Android send data over HTTP and receive body response

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!!.

Upvotes: 1

Views: 902

Answers (1)

user5496907
user5496907

Reputation:

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();
}

Upvotes: 2

Related Questions