2017-03-21 127 views
-3

我的android应用程序抛出JSONException,但我找不出原因! 我使用的是PHP页面,从一个MySQL数据库中选择部分列并把它们放入一个这样的数组:获取JSONException的Android应用程序

$sql = "SELECT name,type,price FROM $tbname"; 
$result = $conn->query($sql); 
$jsonarray=array(); 
if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
$jsonarray[]=$row; 
    } 
$myJSON = json_encode($jsonarray); 
echo $myJSON; 

导致这样的JSON:

[{"name":"sandwich","type":"food","price":"5000"},{"name":"pizza","type":"food","price":"10000"}] 

但是当我尝试在Android Studio中解析上述JSON时,会引发JSONException。 我的Java代码: 我有在课堂上发送数据的方法称为httpmanager如下:

public static String SendData(DataPack pack) throws IOException { 
     URL url=new URL(pack.getUri()); 
     BufferedReader reader=null; 
     HttpURLConnection connection= (HttpURLConnection) url.openConnection(); 
     connection.setDoOutput(true); 
     OutputStreamWriter w=new OutputStreamWriter(connection.getOutputStream()); 
     w.write(pack.Encode()); 
     w.flush(); 
     StringBuilder sb=new StringBuilder(); 
     reader=new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     String line; 
     while((line=reader.readLine())!=null){ 
      sb.append(line + "\n"); 
     } 
     return sb.toString(); 
    } 

然后人工智能拥有的AsyncTask类这样的onpostexecute方法:

protected void onPostExecute(String s) { 
    String jsonStr = s; 
    if (jsonStr != null) { 
     try { 
      JSONObject jsonObj = new JSONObject(jsonStr); 
      Toast.makeText(context, "JSON recieved!", Toast.LENGTH_SHORT).show(); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show(); 
     } 
    } else { 
     Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show(); 
    } 
} 

我会很感激任何关于此事的帮助

+1

后您的Android代码... JSON解析 – rafsanahmad007

+0

您的JSON似乎有效的。发布你的logcat和解析代码 –

+0

你如何解析JSON?你能发布你的Java代码吗? – BlackHatSamurai

回答

0

如何解析de JSON?

您需要使用try/catch方法!

我用的是这样的:

HttpURLConnection connection = null; 
     BufferedReader reader = null; 

     try { 

      URL url = new URL(params[0]); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.connect(); 
      InputStream stream = connection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 

      StringBuffer buffer = new StringBuffer(); 
       String line = ""; 
        while ((line = reader.readLine()) != null) { 
         buffer.append(line); 
        } 

      String finalJson = buffer.toString(); 

      //Code for getting objects/arrays 


    }catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
+0

我有一个try/catch块,但我不明白的是为什么它会抛出异常!? JSON格式似乎是正确的,我认为我解析它就好,但异常不断抛出 – user3770803