Search code examples
javajsonresponsesynchronization

java.io.FileNotFoundException: http://thisismysite/abc.php?usersJSON


I am trying to implement Sync option in my Java application, where I am trying to send all the records in my SQLite database to my remote server and getting back the response using only Java. But unfortunately I am getting these errors. The code below I have posted works fine when run in the availability of internet connection. I'll get these errors when I try to do this:

1) Switch of the internet connection, 2) Insert the 2 or more values/entries into the database, 3) Try to run the code again.

Please help me, Thank you!

Error Log:
    java.io.FileNotFoundException: http://thisismysite/abc.php?usersJSON= 
    [{"respondent_textbox":"hiuh",/*Big json array list goes here */ }]
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1834)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
    at casenotez.NewCase.send(NewCase.java:1286)
    at casenotez.NewCase.save(NewCase.java:906)
    at casenotez.NewCase.btnSaveActionPerformed(NewCase.java:792)
    at casenotez.NewCase.access$2100(NewCase.java:48)
    at casenotez.NewCase$22.actionPerformed(NewCase.java:475)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6527)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6292)  
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4883)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

This is my java code:

    public void send() 
    {
    System.out.println("Entering into send method");   
    try {
    String userList =  this.getAllUsers();
    System.out.println(userList);
    // send as http get request
    URL url = new URL("http://aimzclub.in/Letzkit_Test/test.php?usersJSON="+userList);
    URLConnection conn = url.openConnection();
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    System.out.println("Before while : "+rd);
    while ((line = rd.readLine()) != null) 
    {
    System.out.println("Entering into while loop");
    System.out.println(line);
    }
    rd.close();
    }
    catch (Exception e) 
    {
    e.printStackTrace();
    }
}

Here I am generating array list:

  public String getAllUsers() throws SQLException {
  ArrayList<HashMap<String, String>> wordList;
  wordList = new ArrayList<>();
  String selectQuery = "SELECT  * FROM cnotez";
  ResultSet res = statement.executeQuery(selectQuery);
  System.out.println("This is executing.....");
  if (res.next()) {
       do {
           HashMap<String, String> map = new HashMap<>();
            map.put("case_no_count", res.getString("case_no_count"));
            map.put("court", res.getString("court"));
            map.put("case_no_subject", res.getString("case_no_subject"));
            map.put("case_no_textbox", res.getString("case_no_textbox"));
            map.put("case_no_year", res.getString("case_no_year"));
            map.put("referred_by", res.getString("referred_by"));
            map.put("petition", res.getString("petition"));
            map.put("petition_textbox", res.getString("petition_textbox"));
            map.put("respondent", res.getString("respondent"));
            wordList.add(map);
        } while (res.next());

    }
    Gson gson = new GsonBuilder().create();
    //Use GSON to serialize Array List to JSON
    return gson.toJson(wordList);
    // return wordList;
 }

My PHP server code goes here..... test.php

<?php
include_once './db_functions1.php';
//Create Object for DB_Functions clas
$db = new DB_Functions(); 
//Get JSON posted by Android Application
$json = $_GET["usersJSON"];
 //Remove Slashes
if (get_magic_quotes_gpc())
{
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
   {
   //Store User into MySQL DB
    $res = $db->storeUser($data[$i]->_id,$data[$i]->caseNotez_CN_timestamp,$data[$i]-   
    >our_ref_no,$data[$i]->party_ref_no,$data[$i]->court,$data[$i]->case_no_count,$data[$i]-   
    >referred_by,$data[$i]->petition,$data[$i]->petition_textbox,$data[$i]-
    >case_no_subject,$data[$i]->case_no_textbox,$data[$i]->case_no_year,$data[$i]->respondent);
    //Based on inserttion, create JSON response
}
echo json_encode($data);
?>

db_functions1.php

<?php

class DB_Functions {

private $db;

    function __construct() {
    include_once './db_connect.php';
    // connecting to database
    $this->db = new DB_Connect();
    $this->db->connect();
 }

// destructor
function __destruct() {

}

/**
 * Storing new user
 * returns user details
 */
 public function 
storeUser($_id,$caseNotez_CN_timestamp,$our_ref_no,$party_ref_no,$court,
$case_no_count,$Ref_by,$Petit
ion,$Pet_text,$case_no_subject,$case_no_textbox,$case_no_year,$respondent) 
{
$result = mysql_query("INSERT INTO ABC123 
VALUES($_id,'$caseNotez_CN_timestamp','$our_ref_no','$party_ref_no',
'$court','$case_no_count','$Ref_by','$Petition','$Pet_text',
'$case_no_subject','$case_no_textbox',$case_no_year,'$respondent)");
 if ($result) {
a="Working";
echo json_encode($a);
return true;
}
 else
 {
if( mysql_errno() == 1062) {
// Duplicate key - Primary Key Violation
return true;
        } else {
        $a="Not Working";
    echo json_encode($a);
            // For other errors
            return false;
        }            
    }
  }


}

?>

Solution

  • Finally I found out the solution.

    I was trying to post a Json array list using $_GET method. $_GET was throwing error because it was not able to resolve the spaces between the strings,(Example: "This is my string" ). I just replaced spaces by %20 in my Java code before sending my array list. It worked.

    userList=userList.replaceAll(" ","%20");