Send data to PHP server

Android
voted: 0
vote
android send data php

Easiest way to send data to PHP file on server. You can save this data to the database by prepared PHP file.

public void postData(double data1, double data2, String text) {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setContentCharset(params, "utf-8");
    HttpClient httpclient = new DefaultHttpClient(params);
    HttpPost httppost = new HttpPost("http://file.on.server/add.php");
    httppost.setParams(params);
    
    String data1String = String.valueOf(data1);
    String data2String = String.valueOf(data2);

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("data1", data1String));
        nameValuePairs.add(new BasicNameValuePair("data2", data2String));
        nameValuePairs.add(new BasicNameValuePair("text", text));
        HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
        httppost.setHeader("Accept-Charset", "UTF-8");
        httppost.addHeader(entity.getContentType());
        httppost.setEntity(entity);
        
	ResponseHandler<String> responseHandler = new BasicResponseHandler();
        httpclient.execute(httppost);
	        
    } catch (ClientProtocolException e) {
    	Log.i("Error", e);
    } catch (IOException e) {
    	Log.i("Error", e);
    }
}
working url: not entered
version: not entered
Leave a Comment:
Characters left:
No comments yet.