大家好,我们可以帮助我,因为我永远都会被困在这里,每当我从我的网站主机上获取我的JSON时,我的网站主机上的JSON始终为空,当我使用{检查字符串时{1}}此错误显示Log.d
这是我的代码。
NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object
<?PHP
include_once("connection.php");
session_start();
$where = '';
if (isset($_GET['driverOwner']) && isset($_GET['roles']) && isset ($_GET['driverStatus'])){
$where = " WHERE driverOwner = '".addslashes($_GET['driverOwner'])."' AND roles = '".addslashes($_GET['roles'])."' AND driverStatus = '".addslashes($_GET['driverSTatus'])."'";
}
$query = "SELECT * FROM tbl_user ".$where."";
$result = mysqli_query($conn, $query);
if($result)
{
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
echo json_encode($data);
}
mysqli_close($conn);
?>
public class Drivers1 {
int id;
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Connector {
public static HttpURLConnection connect(String urlAddress){
try{
URL url = new URL(urlAddress);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//SET PROPERTIES
con.setRequestMethod("GET");
con.setConnectTimeout(20000);
con.setReadTimeout(20000);
con.setDoInput(true);
return con;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public class DataParser extends AsyncTask<Void,Void,Integer> {
Context c;
Spinner spDriver;
String jsonData;
ProgressDialog pd;
ArrayList<String> driver = new ArrayList<>();
public DataParser(Context c, Spinner sp, String jsonData) {
this.c = c;
this.spDriver = sp;
this.jsonData = jsonData;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("Fetch");
pd.setMessage("Please wait... Getting drivers");
pd.show();
}
@Override
protected Integer doInBackground(Void... voids) {
return this.parseData();
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
pd.dismiss();
if(result == 0 ){
Toast.makeText(c, "Failed", Toast.LENGTH_SHORT).show();
}else {
//BIND
ArrayAdapter adapter = new ArrayAdapter(c,android.R.layout.simple_list_item_1, driver);
spDriver.setAdapter(adapter);
spDriver.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(c, driver.get(i), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
private int parseData(){
try {
JSONArray ja = new JSONArray(jsonData);
JSONObject jo = null;
driver.clear();
Drivers1 s=null;
for (int i=0;i<ja.length();i++){
jo=ja.getJSONObject(i);
int id=jo.getInt("userID");
String name=jo.getString("firstname");
s = new Drivers1();
s.setId(id);
s.setName(name);
driver.add(name);
}
return 1;
} catch (JSONException e) {
e.printStackTrace();
}
return 0;
}
}
public class Downloader extends AsyncTask<Void,Void,String> {
Context c;
String urlAddress;
Spinner spDriver;
ProgressDialog pd;
public Downloader(Context c, String urlAddress, Spinner spDriver) {
this.c = c;
this.urlAddress = urlAddress;
this.spDriver = spDriver;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("Fetch(Downloader)");
pd.setMessage("Please wait... Getting Drivers");
pd.show();
}
@Override
protected String doInBackground(Void... voids) {
return this.downloadData();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("Downloader",""+ s);
pd.dismiss();
if (s == null) {
Toast.makeText(c, "Unable to retrieve", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(c, "Success!", Toast.LENGTH_SHORT).show();
//CALL PARSER CLASS TO PARSE
DataParser parser = new DataParser(c,spDriver,s);
parser.execute();
}
}
private String downloadData(){
HttpURLConnection con = Connector.connect(urlAddress);
if(con == null){
return null;
}
InputStream is=null;
try {
is = new BufferedInputStream(con.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = "";
StringBuffer response = new StringBuffer();
if (br != null) {
while ((line = br.readLine()) != null){
response.append(line+"\n");
}
}else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
很抱歉很长的帖子但请帮助我。 :c thaaaaanks:)
在Downloader.java上测试
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_insert); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); Spinner spDriver = (Spinner) findViewById(R.id.spDriver); String url = "http://carkila.esy.es/carkila/getDriver.php?driverOwner="+ pref.getString("username","")+"&roles=driver&driverStatus=active"; new Downloader(InsertActivity.this,url ,spDriver).execute(); }
时出错。
Log.d("Downlaoder",s.toString());
答案 0 :(得分:0)
在你的DataParser.Java
班级中,无论如何都会返回0。
private int parseData(){
try {
JSONArray ja = new JSONArray(jsonData);
JSONObject jo = null;
driver.clear();
Drivers1 s=null;
for (int i=0;i<ja.length();i++){
jo=ja.getJSONObject(i);
int id=jo.getInt("userID");
String name=jo.getString("firstname");
s = new Drivers1();
s.setId(id);
s.setName(name);
driver.add(name);
}
return 1;
} catch (JSONException e) {
e.printStackTrace();
}
return 0; //here it will go on
}
请添加日志以检查返回的内容,如果返回0,则必须获得Failed
种吐司。