2017-09-26 71 views
0

我正在尝试在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类

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

你特林访问一个域名为name,但你的JSON没有这一点。替换您的代码jo.getString("name")jo.getString("Skriptname")

+0

你是我的上帝谢谢你<3333我很高兴现在! –

相关问题