我目前正在尝试在ListView中显示我的数据库中的数据。在这个ListView中,我只想从我的表中显示Skriptename。如果我按下按钮 floatingasWord 没有任何反应,我收到一个错误,即DataParser类中没有传递任何名称。
W/System.err: org.json.JSONException: No value for name
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
W/System.err: at
com.ndlp.socialstudy.MySQL.DataParser.parseData(DataParser.java:71)
W/System.err: at
com.ndlp.socialstudy.MySQL.DataParser.doInBackground(DataParser.java:43)
W/System.err: at
com.ndlp.socialstudy.MySQL.DataParser.doInBackground(DataParser.java:20)
我也在这个论坛上看了很多其他类似的话题,但是找不到解决我问题的任何东西。
InformatikskripteActivity:
floatingasWord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Downloader(InformatikSkripteActivity.this, urlAddress, lv_skripteInformatik).execute();
}
});
DownloaderClass
public class Downloader extends AsyncTask<Void,Void,String> {
Context c;
String urlAddress;
ListView lv_skripteInformatik;
ProgressDialog pd;
public Downloader(Context c, String urlAddress, ListView lv_skripteInformatik) {
this.c = c;
this.urlAddress = urlAddress;
this.lv_skripteInformatik = lv_skripteInformatik;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("Retrieve");
pd.setMessage("Retrieving..Please wait");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
return this.downloadData();
}
@Override
protected void onPostExecute(String jsonData) {
super.onPostExecute(jsonData);
pd.dismiss();
if(jsonData.startsWith("Error"))
{
Toast.makeText(c,"Unsuccessful "+jsonData, Toast.LENGTH_SHORT).show();
}else
{
//PARSE
new DataParser(c,jsonData,lv_skripteInformatik).execute();
}
}
private String downloadData()
{
Object connection = Connector.connect(urlAddress);
if(connection.toString().startsWith("Error"))
{
return connection.toString();
}
try {
HttpURLConnection con= (HttpURLConnection) connection;
InputStream is=new BufferedInputStream(con.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer jsonData=new StringBuffer();
while ((line=br.readLine()) != null)
{
jsonData.append(line+"n");
}
br.close();
is.close();
return jsonData.toString();
} catch (IOException e) {
e.printStackTrace();
return "Error "+e.getMessage();
}
}
}
连接器类
public class Connector {
public static Object connect(String urlAddress)
{
try
{
URL url=new URL(urlAddress);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
//SET CON PROPERTIES
con.setRequestMethod("GET");
con.setConnectTimeout(15000);
con.setReadTimeout(15000);
con.setDoInput(true);
return con;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Error "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Error "+e.getMessage();
}
}
}
DataParser Class
public class DataParser extends AsyncTask<Void,Void,Boolean> {
Context c;
String jsonData;
ListView lv_skripteInformatik;
ProgressDialog pd;
ArrayList<String> arrayList=new ArrayList<>();
public DataParser(Context c, String jsonData, ListView lv_skripteInformatik) {
this.c = c;
this.jsonData = jsonData;
this.lv_skripteInformatik = lv_skripteInformatik;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("Parse");
pd.setMessage("Pasring..Please wait");
pd.show();
}
@Override
protected Boolean doInBackground(Void... params) {
return this.parseData();
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pd.dismiss();
if(result)
{
ArrayAdapter adapter=new ArrayAdapter(c,android.R.layout.simple_list_item_1,arrayList);
lv_skripteInformatik.setAdapter(adapter);
lv_skripteInformatik.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(c, arrayList.get(position), Toast.LENGTH_SHORT).show();
}
});
}
}
private Boolean parseData()
{
try
{
JSONArray ja = new JSONArray(jsonData);
JSONObject jo;
arrayList.clear();
for (int i = 0; i < ja.length(); i++) {
jo = ja.getJSONObject(i);
String name = jo.getString("name");
arrayList.add(name);
}
return true;
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
}
PHP脚本:
<?php
$servername = "localhost";
$username = "d027c34a";
$password = "password";
$dbname = "d027c34a";
$con = mysqli_connect($servername, $username, $password, $dbname) or die
('Unable to connect');
if(mysqli_connect_error($con))
{
echo "Failed to Connect to Database".mysqli_connect_error();
}
$sql="SELECT Skriptname FROM skripte";
$result=mysqli_query($con,$sql);
if($result)
{
while($row=mysqli_fetch_array($result))
{
$data[]=$row;
}
print(json_encode($data));
}
mysqli_close($con);
?>
脚本输出:
[{"0":"Matheskript","Skriptname":"Matheskript"},{"0":"Informatikskript","Skriptname":"Informatikskript"}]
答案 0 :(得分:0)
您要访问名为name
的字段,但您的json没有。将代码jo.getString("name")
替换为jo.getString("Skriptname")