2

I want to create a swing application and refer to the database using json.I tried but it did not work and i'm new to json.I want to access the database using webservice. Below my code.

Login.java

String username=jTextField1.getText();
    String password=jPasswordField1.getText();

    JSONObject obj = new JSONObject();

obj.put("username", username);
obj.put("password", password); 



    try {
        HttpClient httpclient= new DefaultHttpClient();
        HttpResponse response;
        HttpPost httppost= new     HttpPost("http://localhost/kolitha/json_test/index.php");
        StringEntity se=new StringEntity ("myjson: "+obj.toString());
        httppost.setEntity(se);
        System.out.print(se);
        httppost.setHeader("Accept", "application/json");
        httppost.setHeader("Content-type", "application/json");

        response=httpclient.execute(httppost);
        String responseBody = EntityUtils.toString(response.getEntity());
        System.out.println("result is "+responseBody);


    }
    catch (Exception e) {
        e.printStackTrace();
        System.out.print("Cannot establish connection!");
    }

index.php here is my php file and i want to get the json object and parse username and password to query and send the response java application.

<?php

$json = file_get_contents('php://input',0,null,null);
$json_output = json_decode($json);

$username;
$password;

    foreach($json_output -> details as $detail)
{
$username = $detail -> username;
$password = $detail -> password;
    }


    $login_result = false;

    $connect = mysql_connect('localhost', 'root', '');
    IF(!$connect)
    {
        die('Failed Connecting to Database: '.mysql_error());
}

$d = mysql_select_db("kolitha_json_test");
    if (!$d)
{
     echo "db not selected";
 }

    $sql = "SELECT * FROM login WHERE username='$username' AND password='$password' ";
    $result = mysql_query($sql) or die (mysql_error());

   if (!$result){
        $login_result = false;
        return $login_result;
        die("Could not run the Query ".mysql_error());
    } else {
        $login_result = true;
        return $login_result;
    }

     ?>
6
  • 1
    what is the error dude? Commented Jul 16, 2013 at 5:07
  • it doesn't return any true or false. i think the fault is in php code. Commented Jul 16, 2013 at 5:12
  • 1
    Did you notice that you're not returning anything from your PHP page to get read in the Java side? Please review your PHP code since this has nothing to do with the Java side. I would recommend you having a JSON response from your PHP page. Here's a solution: How to generate json response using php Commented Jul 16, 2013 at 5:17
  • i return true or false boolean expressions.isn't that enough? Commented Jul 16, 2013 at 5:58
  • @user2508416 looks like you know nothing about web development. A web response can't be a simple boolean nor int nor your type. Maybe you want to use a Web Service instead. Commented Jul 16, 2013 at 16:31

1 Answer 1

1

try adding

request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));

And check if the response has an entity

 HttpResponse response = client.execute(request);
 HttpEntity entity = response.getEntity();

 if (entity != null) {
   InputStream instream = entity.getContent();
   String responseBody = RestClient.convertStreamToString(instream);
    System.out.println("result is "+responseBody);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.