muskan muskan - 6 months ago 19
JSON Question

Not retriving json in return in my android side i had done backend work

I had created webservice and i actually unable to parse it properly . In my script coding is fully done . i am attaching my webservice code .

<?php

{
$tag="viewclass";

require_once 'include/DB_Functions.php';
$db = new DB_Functions();



if ($tag=='viewclass') {

$idofstud = (int)$_POST['idofstud'];
$dayofweek= $_POST['Day'];


$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);


$sql = "SELECT * FROM `mainschedule` WHERE tid=$idofstud and day=\"$dayofweek\"";

$response = array("tag" => $tag, "success" => 0, "error" => 0);

$res = mysqli_query($con,$sql);
/// echo($res);
$result = array();
while($row = mysqli_fetch_array($res)){

array_push($result,
array('scheduleid'=>$row[0],
'day'=>$row[1],
'stime'=>$row[2],
'endtime'=>$row[3],
'subcode'=>$row[4],
'tid'=>$row[5],
'yearof'=>$row[6],
'branchof'=>$row[7],
'sectionof'=>$row[8],
'classkey'=>$row[9],
'classid'=>$row[10],
'classtype'=>$row[11]
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
}


}
?>


My code for parsing json is :-

public JSONObject getJSONFromUrl(String url, List<NameValuePair> 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;

}
}


My url code is

private static String AddSubjectOfTeacher = "http://169.254.90.189/learn2crack_login_api/addteachersubject/teachersubject.php";

My calling function is :-
public JSONObject getTodayLectureofteacher(String s, String dayofweek) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag",lectureofteachertag));
params.add(new BasicNameValuePair("idofstud",s));
params.add(new BasicNameValuePair("Day",dayofweek));

JSONObject json = jsonParser.getJSONFromUrl(LectureofteacherURL, params);




return json;
}


So here backend is complete problem is at android side and i had tried for various times , actually it is not logical error but it seems something is not properly attached .

Answer

I think you had not called that function from a specific java code from which it must be called just call your method like these :-

  JSONObject json = getTodayLectureofteacher(uniq,todayda);
Comments