I'm trying to create a login application on android and I get an error telling me that the JSON object is null.
I get the error from here :
This is my JSONParser:
And this is my index.php :
This is what I'm sending and receiving:
The website works because I tested it with Postman, so the problem is within the JSONParser.
I get the error from here :
Code:
public JSONObject loginUser(String email, String password){
// Building Parameters
List<NameValuePair> 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);
if (json == null) {
Log.e("JSON", "Object is null!!!");
}
return json;
}
Code:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
private String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
return sb.toString();
}
public JSONObject getJSONFromUrl(String url, List params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
for (int i=0; i<params.size(); i++) {
Log.v("askj",params.get(i).toString());
}
httpPost.setEntity(new UrlEncodedFormEntity(params));
try {
Log.v("askj","HTTP Entity : " + convertStreamToString(httpPost.getEntity().getContent()));
}catch (Exception e){
e.printStackTrace();
}
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.v("askj",json);
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;
}
}
And this is my index.php :
Code:
<?php echo json_encode($_POST); exit;?>
This is what I'm sending and receiving:
Code:
05-14 10:49:26.547 27963-28074/name.company.project V/askj﹕ tag=login
05-14 10:49:26.547 27963-28074/name.company.project V/askj﹕ email=emailTest
05-14 10:49:26.547 27963-28074/name.company.project V/askj﹕ password=passTest
05-14 10:49:26.557 27963-28074/name.company.project V/askj﹕ HTTP Entity sent : tag=login&email=emailTest&password=passTest
05-14 10:49:27.117 27963-28074/name.company.project V/askj﹕ json received: []
The website works because I tested it with Postman, so the problem is within the JSONParser.
Last edited: