Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to fetch correct json string
    primarykey
    data
    text
    <p>I am currently working an android app that login users.Upon login, users are allowed name their own create a file. However, upon executing this function as shown below, the return json contain the JSON string of the login details instead.</p> <p>I am using a xmapp server.</p> <p>Does anyone have any idea of the correct way of obtaining the returned json string of the createNewFile details ?</p> <pre><code>public JSONObject createNewFile(String f_id, String userid, String name){ // Building Parameters List params= new ArrayList(); params.add(new BasicNameValuePair("tag", createFile)); params.add(new BasicNameValuePair("fid", fid)); params.add(new BasicNameValuePair("userid", userid)); params.add(new BasicNameValuePair("name", name)); // getting JSON Object JSONObject json = jsonParser.getJSONFromUrl(URL, params); // return json return json; } </code></pre> <p>//use for login</p> <pre><code>public JSONObject loginUsers(String email, String password){ // Building Parameters List params = new ArrayList(); params.add(new BasicNameValuePair("tag", login_tag)); params.add(new BasicNameValuePair("email", email)); params.add(new BasicNameValuePair("password", password)); JSONObject json = jsonParser.getJSONFromUrl(loginURL, params); // return json Log.e("JSON", json.toString()); return json; } </code></pre> <p>This is my <code>JSONPARSER</code> class.</p> <pre><code>public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url, List 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 + "n"); } is.close(); json = sb.toString(); Log.e("JSON", 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; } } </code></pre> <p>Php files:</p> <p>DB_Functions</p> <pre><code>public function storeFile($fid, $userid, $name){ $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD); mysqli_select_db($con, DB_DATABASE); $result = mysqli_query($con,"INSERT INTO file_info(fid, userid, name) VALUES('$fid', '$userid', '$name', NOW())"); // check for successful store if ($result) { // get file details $fid = mysqli_insert_id($con); // last inserted id $result = mysqli_query($con,"SELECT * FROM file_info WHERE fid = $fid"); // return file details return mysqli_fetch_array($result); } else { return false; } } </code></pre> <p>index.php</p> <pre><code> //add_file if ($tag == 'add_file') { // Request type is add new file $fid = $_POST['fid']; $userid = $_POST['userid']; $name = $_POST['name']; $file_info= $db-&gt;storeFile($fid, $userid, $name); if ($file_info) { // user stored successfully $response["success"] = 1; $response["fid"] = $file_info["fid"]; $response["file_info"]["userid"] = $file_info["userid"]; $response["file_info"]["name"] = $ufile_info["name"]; echo json_encode($response); } else { // user failed to store $response["error"] = 1; $response["error_msg"] = "Error occured in Adding"; echo json_encode($response); } }else { echo "Invalid Request"; } } else { echo "Access Denied"; } </code></pre> <p>Error faced: </p> <pre><code>Invalid RequestInvalid Requestn Error parsing data org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload