I tried to create online applications. I would like to send a string from my applications to my PHP script, but in the end php did not get a single line from my applications, which means that PhP will return NULL. I did research online and looked for a solution, but none of them worked.
Below is my MainActivity.java code:
package my.com.tutionathome.calvinlau.testserver; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { Button b; EditText et; TextView tv; HttpPost httppost; StringBuffer buffer; HttpResponse response; HttpClient httpclient; List<NameValuePair> nameValuePairs; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b = (Button)findViewById(R.id.Button01); et= (EditText)findViewById(R.id.EditText01); tv= (TextView)findViewById(R.id.tv); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final ProgressDialog p = new ProgressDialog(v.getContext()).show(v.getContext(),"Waiting for Server", "Accessing Server"); Thread thread = new Thread() { @Override public void run() { try{ httpclient=new DefaultHttpClient(); httppost= new HttpPost("http://10.0.0.2/my_folder_inside_htdocs/connection.php");
And my php code:
<?php // put your code here $hostname_localhost ="localhost"; $database_localhost ="android"; $username_localhost ="root"; $password_localhost =""; $localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost) or trigger_error(mysql_error(),E_USER_ERROR); mysql_select_db($database_localhost, $localhost); $username = $_POST['username']; $password = $_POST['password']; $query_search = "select Email, Username from tblmember where Username = '".$username."' AND Email = '".$password. "'"; $query_exec = mysql_query($query_search) or die(mysql_error()); $rows = mysql_num_rows($query_exec); if($username == NULL){ echo "NULL"; }else{ echo $username; } ?>
This is my Android manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.com.tutionathome.calvinlau.testserver"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
source share