jidhin tr

jidhin tr

  • NA
  • 16
  • 7k

Aprogram for android log in with wamp server

Aug 7 2013 1:33 AM
I establish the connection with php , inserted values too.
now i wanna check that whether the user is registered or not.

for me I am getting successful if credentials are ok, else part is nt wrkng for  me.


here by i am attaching the code too.

--------------------------------------------------------------------------------------------------------------------------------------------------

package com.example.wamplogin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

EditText ed1, ed2;
int p = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
}

public void loginphp(View v) {
String name = ed1.getText().toString();
String pass = ed2.getText().toString();
ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("name", name));
list.add(new BasicNameValuePair("password", pass));

DefaultHttpClient dtp = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.0.2.2:80/test/login.php");
HttpEntity ent;
try {
post.setEntity(new UrlEncodedFormEntity(list));
HttpResponse resp = dtp.execute(post);
ent = resp.getEntity();
InputStream is = ent.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str;
StringBuilder sb = new StringBuilder();

while ((str = br.readLine()) != null) {
sb.append(str + "\n");

}
str = sb.toString();

JSONArray jaary = new JSONArray(str);
int len = jaary.length();
TextView tv = (TextView) findViewById(R.id.textView1);

for (int i = 0; i < len; i++) {
JSONObject o = jaary.getJSONObject(i);
if (o.getString("name").equals(name)
&& o.getString("password").equals(pass)) {
tv.setText("ok");
Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
p = 1;
break;
}

}

// Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (p == 0) {
Toast.makeText(this, "failes ", Toast.LENGTH_SHORT).show();
}
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
 --------------------------------------------------------------------------------------
php code ---------------------------------
------------------------------------------------------------------------------------------


<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$q=mysql_query("select * from login where name = '".$_REQUEST['name']."'and password = '".$_REQUEST['password']."'");
while($r=mysql_fetch_assoc($q))
$output[]=$r;
print(json_encode($output));
mysql_close();
?>
--------------------- End-------------------

Answers (1)