i am webscrapping a website for education only and trying to show returned JsonResponse but while app runs, it throws an error org.json.JSONException: Value Error of type java.lang.String cannot be converted to JSONObject. i found several questions similar to this but i didn't got any useful answer and they also contains incomplete inforrmation about problem. following are codes that might a cause for this error. i hope that this thread will also help others because i am trying to give more information regarding to this problem.
MainActivity.java
private void apicall() {
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
final StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject1 = new JSONObject(response);
if (jsonObject1.getString("active").equals("0")) {
Toast.makeText(MainActivity.this, "Your account temporarily disabled by admin", Toast.LENGTH_SHORT).show();
preferences.edit().clear().apply();
Intent in = new Intent(getApplicationContext(), login.class);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
finish();
}
if (!jsonObject1.getString("session").equals(getSharedPreferences(constant.prefs, MODE_PRIVATE).getString("session", null))) {
Toast.makeText(MainActivity.this, "Session expired ! Please login again", Toast.LENGTH_SHORT).show();
preferences.edit().clear().apply();
Intent in = new Intent(getApplicationContext(), login.class);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
finish();
}
balance.setText(jsonObject1.getString("wallet"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
hometext.setText(Html.fromHtml(jsonObject1.getString("homeline"), Html.FROM_HTML_MODE_COMPACT));
} else {
hometext.setText(Html.fromHtml(jsonObject1.getString("homeline")));
}
ArrayList<String> name = new ArrayList<>();
ArrayList<String> result = new ArrayList<>();
JSONArray jsonArray = jsonObject1.getJSONArray("result");
for (int a = 0; a < jsonArray.length(); a++){
JSONObject jsonObject = jsonArray.getJSONObject(a);
name.add(jsonObject.getString("market"));
result.add(jsonObject.getString("result"));
}
adapter_result rc = new adapter_result(MainActivity.this,name,result);
recyclerview.setLayoutManager(new GridLayoutManager(MainActivity.this, 2));
recyclerview.setAdapter(rc);
rc.notifyDataSetChanged();
SharedPreferences.Editor editor = preferences.edit();
editor.putString("wallet", jsonObject1.getString("wallet")).apply();
editor.putString("homeline", jsonObject1.getString("homeline")).apply();
editor.putString("code", jsonObject1.getString("code")).apply();
is_gateway = jsonObject1.getString("gateway");
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(MainActivity.this, "Check your internet connection", Toast.LENGTH_SHORT).show();
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("mobile", preferences.getString("mobile", null));
params.put("session",getSharedPreferences(constant.prefs, MODE_PRIVATE).getString("session", null));
return params;
}
};
postRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(postRequest);
}
home.php
<?php
include "../connection/config.php";
extract($_REQUEST);
// if(rows(query("select sn from users where mobile='$mobile' and session ='$session'")) == 0){
// $data['msg'] = "You are not authrized to use this";
// $dd = query("select session,active from users where mobile='$mobile'");
// $d = fetch($dd);
// $data['session'] = $d['session'];
// $data['active'] = $d['active'];
// echo json_encode($data);
// return;
// }
require_once "../scrap/data/support/web_browser.php";
require_once "../scrap/data/support/tag_filter.php";
$htmloptions = TagFilter::GetHTMLOptions();
$url = "https://dpboss.mobi/";
$web = new WebBrowser();
$result = $web->Process($url);
if (!$result["success"])
{
echo "Error retrieving URL. " . $result["error"] . "\n";
exit();
}
if ($result["response"]["code"] != 200)
{
echo "Error retrieving URL. Server returned: " . $result["response"]["code"] . " " . $result["response"]["meaning"] . "\n";
exit();
}
$baseurl = $result["url"];
$html = TagFilter::Explode($result["body"], $htmloptions);
$root = $html->Get();
$rows = $root->Find("div.satta-result h4");
$h5rows = $root->Find("div.satta-result h5");
foreach ($rows as $row)
{
$temp_h4[] = $row->GetInnerHTML();
}
foreach ($h5rows as $row)
{
$temp_h5[] = $row->GetInnerHTML();
}
$get = query("select * from gametime_new");
while($xc = fetch($get))
{
$time = array_search($xc['market'], $temp_h4);
$mrk['market'] = $xc['market'];
$mrk['result'] = $temp_h5[$time];
$data['result'][] = $mrk;
}
$dd = query("select sn,wallet,active,session,code from users where mobile='$mobile'");
$d = fetch($dd);
$nt = query("select homeline from content where sn='1'");
$n = fetch($nt);
if($d['code'] == "0")
{
$code = $d['sn'].rand(100000,9999999);
query("update users set code='$code' where mobile='$mobile'");
}
else
{
$code = $d['code'];
}
if(rows(query("select sn from gateway_config where active='1'")) > 0){
$data['gateway'] = "1";
} else {
$data['gateway'] = "0";
}
$data['code'] = $code;
$data['wallet'] = $d['wallet'];
$data['active'] = $d['active'];
$data['session'] = $d['session'];
$data['homeline'] = $n['homeline'];
echo json_encode($data);
JsonResponse
{"result":[{"market":"SRIDEVI","result":"249-54-356"},{"market":"TIME BAZAR","result":"568-98-279"},{"market":"MILAN DAY","result":"133-74-248"},{"market":"RAJDHANI DAY","result":"123-63-148"},{"market":"KALYAN","result":"266-44-130"},{"market":"SRIDEVI NIGHT","result":"588-11-489"},{"market":"NIGHT TIME BAZAR","result":"258-52-129"},{"market":"MILAN NIGHT","result":"568-98-440"},{"market":"KALYAN NIGHT","result":"447-57-188"},{"market":"RAJDHANI NIGHT","result":"458-76-790"},{"market":"MAIN BAZAR","result":"377-76-367"},{"market":"WORLI MUMBAI","result":"124-70-145"},{"market":"PUNA BAZAR","result":"350-89-379"},{"market":"MILAN MORNING","result":"123-63-148"}],"gateway":"1","code":null,"wallet":null,"active":null,"session":null,"homeline":"WELCOME TO BETPLAY"}