below i have posted my JSONParser code:
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "
");
}
is.close();
json = sb.toString();
Log.e("JSONN ", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
here i use my function to start login :
public JSONObject loginUser(String username, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL+login_tag+".php", params);
return json;
}
i use my functions in an Async task like below:
public void login(View view){
new ProcessLogin().execute();
}
/**
* Async Task to get and send data to My Sql database through JSON respone.
**/
private class ProcessLogin extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
String username, password;
@Override
protected void onPreExecute() {
super.onPreExecute();
usernameField = (EditText) findViewById(R.id.editText1);
passwordField = (EditText) findViewById(R.id.editText2);
username = usernameField.getText().toString();
password = passwordField.getText().toString();
}
@Override
protected JSONObject doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(username, password);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
int code = json.getInt("code");
Log.d("myLog", "JSON get code :" + String.valueOf(code));
if (code == 1) {
Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_LONG).show();
Intent upanel = new Intent(getApplicationContext(), MainActivity.class);
finish();
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
here is my php code:
<?php
$con=mysqli_connect("localhost","root","","my_db");
//echo "Welcome, I am connecting Android to PHP, MySQL";
if (mysqli_connect_errno($con))
{
//echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//$username = 'admin';
//$password = 'admin';
$username = $_POST['username'];
$password = $_POST['password'];
//Für die Überprüfung
$result = mysqli_query($con,"SELECT * FROM user where Username='$username' and Password='$password'");
//if($result){
//$user = $user_details
//$flag['code'] =0;
$response['code'] = 0;
if(mysqli_num_rows($result)==1){
$flag['code']=1;
//Für UserDetails
$user_details = mysqli_fetch_array($result);
//User wird im JSON Object gespeichert
$response["code"] = 1;
$response["user"]["user_id"] = $user_details["user_id"];
$response["user"]["username"] = $user_details["Username"];
$response["user"]["password"] = $user_details["Password"];
$response["user"]["email"] = $user_details["E-Mail"];
} else {
$response['code'] = 0;
//$flag['code']=0;
}
// }
//print(json_encode($flag));
//print(json_encode($user_details));
print json_encode($response);
mysqli_close($con);
?>
i know that it would be better to hash my passwords, but i want to do this later on. i added more logs and now i have this log :
08-18 14:03:14.313 1444-1459/? E/HTTPClient﹕ org.apache.http.conn.EofSensorInputStream@1abaea2f 08-18 14:03:14.317 1444-1459/? E/Buffered Reader﹕ java.io.BufferedReader@244a383c 08-18 14:03:14.322 1444-1459/? E/String Builder﹕ {"code":1,"user":{"user_id":"3","username":"david","password":"david","email":"david"}} 08-18 14:03:14.322 1444-1459/? E/JSON﹕ {"code":1,"user":{"user_id":"3","username":"david","password":"david","email":"david"}} 08-18 14:03:14.439 55-55/? D/gralloc﹕ Registering a buffer in the process that created it. This may cause memory ordering problems. 08-18 14:03:14.439 55-55/? E/libEGL﹕ called unimplemented OpenGL ES API 08-18 14:03:14.440 55-55/? E/SurfaceFlinger﹕ glCheckFramebufferStatusOES error 0 08-18 14:03:14.440 55-55/? E/SurfaceFlinger﹕ got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
the app doesnt make screenshots or something like this. i think the error could be with the JSON Object
this is just a part of it , but i think this should be enough.
thanks for any helps or advises