android - Check if null value is retrieved from an external service -
android - Check if null value is retrieved from an external service -
i´m retrieving values external database android application doing phone call on service that's on server. here's php file:
$mysqli = new mysqli(db_host, db_user, db_password, db_name); if (!$mysqli) { die('connect error (' . mysqli_connect_errno() . ') ' . sqli_connect_error()); } $q=mysqli_query($mysqli, "select title post upper(title) upper('%" . $_request['searchthis'] . "%')"); while($e=mysqli_fetch_assoc($q)){ $output[]=$e; } print(json_encode($output)); mysqli_close();
and class (i wrote code matters):
try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch(exception e){ log.e("connection_error", "error in http connection "+e.tostring()); } 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(); result=sb.tostring(); ///-------------system outs--------------- system.out.println(result); system.out.println(result.getclass().getname()); } catch(exception e){ log.e("buffer_error", "error converting result "+e.tostring()); } //------------------problems--------------- if(result.equalsignorecase("null")){ list.add("empty"); } else{ try{ jsonarray jarray = new jsonarray(result); for(int i=0;i<jarray.length();i++){ jsonobject json_data = jarray.getjsonobject(i); //log.i("result",json_data.getstring("title")); string title = json_data.getstring("title"); list.add(title); } } catch(jsonexception e){ log.e("data_parsing_error", "error parsing info "+e.tostring()); } } homecoming list;
}
i've tried whit logs , prints , i'm getting is:
null //for system.out.println(result); java.lang.string //for system.out.println(result.getclass().getname());
it works when php service on server returns something, problem when compare result "null" it's false though result string value "null". of course of study run json exception because result isn't valid format conversion. perchance wrong here?
your code not seem consider trailing newline character ('\n') appended you.
you can either remove appending of newline character ('\n') in while loop or remove when check "null".
android json web-services
Comments
Post a Comment