Android : Null Pointer Exception while retrieving json object using php?

on Friday, October 31, 2014


android code:



protected String doInBackground(String... params) {
// TODO Auto-generated method stub

try {
SharedPreferences prefs = getActivity().getSharedPreferences("Login_Prefs", 0);

username = prefs.getString("username", "defValue");

String url = "http://www.iloveexpressions.com/eh/viewFriendList.php";

nameValuePairs = new ArrayList<NameValuePair>(1);

Log.d("Parameters ", username);

nameValuePairs.add(new BasicNameValuePair("username", username.trim()));

Log.d("name value pairs", nameValuePairs.toString());

JSONObject json = jsonParser.makeHttpRequest(url, "POST",
nameValuePairs);

Log.d("Printing after json", json.toString());

success = json.getString("success");

Log.d("Success : ", success);

} catch (Exception e) {
Log.d("Error : ", e.toString());
}


php code:



<?php

require 'connection.php';

$response = array();

if(isset($_POST["username"]))
{
$username = $_POST["username"];

$response["success"] = 1;

echo json_encode($response);
}
else
{
$response["success"] = 0;

echo json_encode($response);
}

?>


This is the error i get in log :



Parameters(30889): aaa

name value pairs(30889): [username=aaa]

Error :(30889): java.lang.NullPointerException


How do I solve this error ? I am using the same method to send and retrieve data using php on android and it works fine ? I am returning success if username is sent to php file correctly


I am using the doInBackground process in a fragment and calling the execute() method in the onCreateView() method is this possible ?


0 comments:

Post a Comment