【发布时间】:2016-04-07 04:09:52
【问题描述】:
这是json代码,httpurlconnection从json获取数据
protected Void doInBackground(Void...params){
InputStream is=null;
String result="";
String urlDate="http://pangkortourism.ga/fyp/player.php";
BufferedReader reader=null;
try{
URL urlp=new URL(urlDate);
try {
HttpURLConnection c = (HttpURLConnection) urlp.openConnection();
c.setRequestMethod("GET");
c.setReadTimeout(10000);
c.connect();
reader = new BufferedReader(new InputStreamReader(c.getInputStream(), "UTF-8"));
StringBuilder buf = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
buf.append(line);
}
result = buf.toString();
}
finally {
if (reader != null) {
reader.close();
}
}
} catch (IOException e) {
Log.e("MalformedURLException", "MalformedURLException " + e.toString());
}
//parse json data
try{
// Remove unexpected characters that might be added to beginning of the string
result=result.substring(result.indexOf("["));
JSONArray jArray =new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Product p=new Product();
p.setid(json_data.getString("id"));
p.setitem(json_data.getInt("item"));
records.add(p);
}
}
catch(Exception e){
Log.e("ERROR", "Error pasting data "+e.toString());
}
return null;
}
mysql数据库数据
json 代码
[
{"0":"1","id":"1","1":"pizza","item":"pizza"},
{"0":"2","id":"2","1":"burger","item":"burger"}
]
android studio 中的错误
04-07 11:58:12.527 23232-23303/com.khor.newtry6 E/ERROR: 粘贴错误 数据 org.json.JSONException:值披萨类型的项目 java.lang.String 无法转换为 int 04-07 11:58:12.542 23232-23232/com.khor.newtry6 E/大小:0
【问题讨论】:
-
json_data.getInt("item"));项目不是数字
-
我先回答了问题,然后复制粘贴:P