storing data at remote server using php
- by VIPUL GOYAL
I want to send data to php file and execute insert query in it to store data in database.
When i execute this code, it executes but database does not get updated.
Code of PHP file
Java code------------------------------Remote.java------------------------------
public class Remote extends Activity {
EditText name;
Button s;
String r;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name= (EditText)findViewById(R.id.editText1);
s=(Button)findViewById(R.id.button1);
s.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
r = name.getText().toString();
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", r));
CustomHttpClient.executeHttpPost("http://vipul.eu5.org/abc.php", postParameters); //Enetr Your remote PHP,ASP, Servlet file link
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
-----------------------------CustomHttpClient.java----------------------------
public class CustomHttpClient {
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
/**
* Performs an HTTP Post request to the specified url with the
* specified parameters.
*
* @param url The web address to post the request to
* @param postParameters The parameters to send via the request
* @return The result of the request
* @throws Exception
*/
public static void executeHttpPost(String url, ArrayList postParameters) throws Exception {
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
}
catch(Exception e)
{
}
}
}