Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.05 KB | None | 0 0
  1. <?php
  2. $host='localhost';
  3. $uname='amodbina0106';
  4. $pwd='Amodbina200';
  5. $db="kezin_king";
  6.  
  7. $con=mysqli_connect("localhost","amodbina0106","Amodbina200","kezin_king");
  8. if ($con->connect_error) {
  9. die("Connection failed: " . $con->connect_error);
  10. }
  11. echo "Connection Successful";
  12.  
  13.  
  14. $username = $_GET['username'];
  15. $password = $_GET['password'];
  16. $flag['code']=0;
  17.  
  18. if($name == '' || $username == '' || $password == '' || $email == ''){
  19. echo 'please fill all values';
  20. }
  21. else{
  22.  
  23.  
  24.  
  25. $sql=mysql_query("insert into sample values('$id','$name') ",$con);
  26.  
  27. if(mysqli_query($con,$sql))
  28.  
  29. {
  30. $flag['code']=1;
  31. echo"hi";
  32. }
  33.  
  34. print(json_encode($flag));
  35. mysql_close($con);
  36. }
  37. ?>
  38.  
  39. **Output :-**
  40. [1]: http://kezinking.com/SampleConLogin
  41.  
  42. package com.example.nupur.phpdb_con;
  43.  
  44. import android.content.Context;
  45. import android.support.v7.app.AppCompatActivity;
  46. import android.os.Bundle;
  47. import android.util.Log;
  48. import android.view.Menu;
  49. import android.view.View;
  50. import android.widget.Button;
  51. import android.widget.EditText;
  52. import android.widget.TextView;
  53. import android.widget.Toast;
  54.  
  55. import org.apache.http.HttpEntity;
  56. import org.apache.http.HttpResponse;
  57. import org.apache.http.NameValuePair;
  58. import org.apache.http.client.HttpClient;
  59. import org.apache.http.client.entity.UrlEncodedFormEntity;
  60. import org.apache.http.client.methods.HttpGet;
  61. import org.apache.http.client.methods.HttpPost;
  62. import org.apache.http.impl.client.DefaultHttpClient;
  63. import org.apache.http.message.BasicNameValuePair;
  64. import org.json.JSONObject;
  65.  
  66. import java.io.BufferedReader;
  67. import java.io.IOException;
  68. import java.io.InputStream;
  69. import java.io.InputStreamReader;
  70. import java.io.UnsupportedEncodingException;
  71. import java.nio.Buffer;
  72. import java.util.ArrayList;
  73. import org.json.JSONArray;
  74. import org.json.JSONException;
  75. import org.json.JSONObject;
  76. import java.io.BufferedReader;
  77. import java.io.InputStream;
  78. import java.io.InputStreamReader;
  79.  
  80.  
  81.  
  82. public class MainActivity extends AppCompatActivity {
  83. EditText etUsername, etPassword;
  84. TextView txtView;
  85. Button btnLogin;
  86. String username;
  87. String password;
  88. InputStream is = null;
  89. String result = "";
  90. String line = null;
  91. int code;
  92. InputStream inputStream = null;
  93. BufferedReader reader;
  94.  
  95. @Override
  96. protected void onCreate(Bundle savedInstanceState) {
  97. super.onCreate(savedInstanceState);
  98. setContentView(R.layout.activity_main);
  99. etUsername = (EditText) findViewById(R.id.etUsername);
  100. etPassword = (EditText) findViewById(R.id.etPassword);
  101. btnLogin = (Button) findViewById(R.id.btnLogin);
  102. txtView = (TextView) findViewById(R.id.txtView);
  103. btnLogin.setOnClickListener(new View.OnClickListener() {
  104. @Override
  105. public void onClick(View view) {
  106. username = etUsername.getText().toString();
  107. password = etPassword.getText().toString();
  108. insert();
  109.  
  110.  
  111. // txtView.setText(etUsername.getText()+" "+etPassword.getText());
  112.  
  113.  
  114. }
  115. });
  116.  
  117.  
  118. }
  119.  
  120. public String insert() {
  121. BufferedReader bufferedReader;
  122.  
  123. ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
  124. nameValuePairs.add(new BasicNameValuePair("Username", username));
  125. nameValuePairs.add(new BasicNameValuePair("Password", password));
  126. try {
  127.  
  128. // create HttpClient
  129. HttpClient httpclient = new DefaultHttpClient();
  130.  
  131. // make GET request to the given URL
  132. HttpResponse httpResponse = httpclient.execute(new HttpGet("http://kezinking.com/SampleConLogin"));
  133.  
  134. // receive response as inputStream
  135. inputStream = httpResponse.getEntity().getContent();
  136.  
  137. // convert inputstream to string
  138. if (inputStream != null) {
  139. bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  140. while ((line = bufferedReader.readLine()) != null)
  141. result += line;
  142.  
  143. inputStream.close();
  144. return result;
  145. } else
  146. result = "Did not work!";
  147.  
  148. } catch (Exception e) {
  149. Log.e("Fail 1", e.toString());
  150. Toast.makeText(getApplicationContext(), "Invalid IP Address",
  151. Toast.LENGTH_LONG).show();
  152. }
  153.  
  154. try {
  155.  
  156. BufferedReader reader = new BufferedReader
  157. (new InputStreamReader(is, "UTF-8"), 8);
  158. StringBuilder sb = new StringBuilder();
  159. while ((line = reader.readLine()) != null) {
  160. sb.append(line + "n");
  161. }
  162. is.close();
  163. result = sb.toString();
  164. Log.e("pass 2", "connection success ");
  165. } catch (Exception e) {
  166. Log.e("Fail 2", e.toString());
  167. }
  168. try {
  169. JSONObject json_data = new JSONObject(result);
  170. code = (json_data.getInt("code"));
  171. if (code == 1) {
  172. Toast.makeText(getBaseContext(), "Inserted Successfully",
  173. Toast.LENGTH_SHORT).show();
  174. } else {
  175. Toast.makeText(getBaseContext(), "Sorry, Try Again",
  176. Toast.LENGTH_LONG).show();
  177. }
  178. } catch (Exception e) {
  179. Log.e("Fail 3", e.toString());
  180.  
  181. }
  182.  
  183. return null;
  184. }
  185.  
  186. // @Override
  187. //public boolean onCreateOptionsMenu(Menu menu) {
  188. // getMenuInflater().inflate(R.menu.activity_main, menu);
  189. //return true;
  190. //}
  191.  
  192. }
  193.  
  194. <?xml version="1.0" encoding="utf-8"?>
  195. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  196. xmlns:app="http://schemas.android.com/apk/res-auto"
  197. xmlns:tools="http://schemas.android.com/tools"
  198. android:id="@+id/activity_main"
  199. android:layout_width="match_parent"
  200. android:layout_height="match_parent"
  201. tools:context="com.example.nupur.phpdb_con.MainActivity">
  202. <LinearLayout
  203. android:layout_width="wrap_content"
  204. android:layout_height="wrap_content">
  205. <EditText
  206. android:layout_width="fill_parent"
  207. android:layout_height="wrap_content"
  208. android:ems="10"
  209. android:id="@+id/etUsername"
  210. android:layout_marginBottom="10px"
  211.  
  212. android:hint="UserName"/>
  213. </LinearLayout>
  214. <LinearLayout
  215. android:layout_width="wrap_content"
  216. android:layout_height="wrap_content">
  217. <EditText
  218. android:layout_width="wrap_content"
  219. android:layout_height="wrap_content"
  220. android:id="@+id/etPassword"
  221. android:ems="10"
  222. android:layout_marginTop="100px"
  223. android:hint="Password" />
  224. </LinearLayout>
  225. <LinearLayout
  226. android:layout_width="wrap_content"
  227. android:layout_height="wrap_content">
  228. <Button
  229. android:id="@+id/btnLogin"
  230. android:layout_width="wrap_content"
  231. android:layout_height="wrap_content"
  232. android:layout_marginLeft="250px"
  233. android:gravity="center"
  234. android:text="@string/Login"
  235. android:layout_marginTop="400px"/>
  236. </LinearLayout>
  237. <TextView
  238. android:gravity="bottom"
  239. android:id="@+id/txtView"
  240. android:layout_width="wrap_content"
  241. android:layout_height="wrap_content" />
  242.  
  243. </android.support.constraint.ConstraintLayout>
  244.  
  245. <?xml version="1.0" encoding="utf-8"?>
  246. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  247. package="com.example.nupur.phpdb_con">
  248. <uses-permission android:name="android.permission.INTERNET"/>
  249. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  250.  
  251. <application
  252. android:allowBackup="true"
  253. android:icon="@mipmap/ic_launcher"
  254. android:label="@string/app_name"
  255. android:supportsRtl="true"
  256. android:theme="@style/AppTheme">
  257. <activity android:name=".MainActivity">
  258. <intent-filter>
  259. <action android:name="android.intent.action.MAIN" />
  260.  
  261. <category android:name="android.intent.category.LAUNCHER" />
  262. </intent-filter>
  263. </activity>
  264. </application>
  265.  
  266. </manifest>
  267.  
  268. Error in Emulator:-
  269. Invalid Ip Address
  270.  
  271.  
  272.  
  273.  
  274. Thanks for helping me..(In Advance..)
  275.  
  276. <?php
  277. $host='localhost';
  278. $uname='amodbina0106';
  279. $pwd='Amodbina200';
  280. $db="kezin_king";
  281. $result = array();
  282. $con=mysqli_connect("localhost","amodbina0106","Amodbina200","kezin_king");
  283. if ($con->connect_error) {
  284. $result['status'] = false;
  285. $result['message'] = 'server lost';
  286. die(json_encode($result));
  287. }
  288.  
  289.  
  290. $username = $_GET['username'];
  291. $password = $_GET['password'];
  292. $flag['code']=0;
  293.  
  294. if($name == '' || $username == '' || $password == '' || $email == ''){
  295. $result['status'] = false;
  296. $result['message'] = 'All field required';
  297. }
  298. else{
  299.  
  300.  
  301.  
  302. $sql=mysql_query("insert into sample values('$id','$name') ",$con);
  303.  
  304. if(mysqli_query($con,$sql))
  305.  
  306. {
  307. $flag['code']=1;
  308. $result['status'] = true;
  309. $result['message'] = 'hi';
  310. }
  311.  
  312. print(json_encode($result));
  313. mysql_close($con);
  314. }
  315. ?>
  316.  
  317. new insertData().execute(namevaluepair);
  318.  
  319. private class insertData extends AsyncTask<Arraylist<NameValuePair>, Void, String> { @Override
  320. protected String doInBackground(Arraylist<NameValuePair>... params) {
  321. ArrayList<NameValuePair> namevaluepair=params[0];
  322.  
  323. //make http request here
  324. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement