2013-11-14 41 views
0

我想執行一個小型的android應用程序來獲取數據庫中承載的Web服務的數據,有以下大量的代碼,但我得到一些錯誤。錯誤解析數據,JSON

MainActivity.java

public class MainActivity extends Activity { 

TextView Name; 
TextView ID; 
TextView Email; 
private static String url = "http://HostName/GetData.php"; 
private static final String TAG_Name = "Name"; 
private static final String TAG_ID = "Id"; 
private static final String TAG_Email = "Email"; 

JSONArray user = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    new JSONParse().execute(); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
private class JSONParse extends AsyncTask<String, String, JSONObject> 
{ 
    private ProgressDialog pDialog; 

    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     Name = (TextView) findViewById(R.id.textView4); 
     ID = (TextView) findViewById(R.id.textView5); 
     Email = (TextView) findViewById(R.id.textView6); 

     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Getting Data ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected JSONObject doInBackground(String... args) { 
     JSONParser jParser = new JSONParser(); 

     // Getting JSON from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 
     return json; 
    } 

    @Override 
    protected void onPostExecute(JSONObject json) { 
     pDialog.dismiss(); 
     try { 
       // Getting JSON Array 
       user = json.getJSONArray(TAG_Name); 
       JSONObject c = user.getJSONObject(0); 

       // Storing JSON item in a Variable 
       String Name1 = c.getString(TAG_Name); 
       String ID1 = c.getString(TAG_ID); 
       String Email1 = c.getString(TAG_Email); 

       //Set JSON Data in TextView 
       Name.setText(Name1); 
       ID.setText(ID1); 
       Email.setText(Email1); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 

} 
} 

JSONParser.java:

public class JSONParser { 
static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

// constructor 
public JSONParser() { 

} 

public JSONObject getJSONFromUrl(String url) { 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent();   

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 
} 

}

我有以下的logcat:

LogCat

PHP文件:

<?php 
header('Content-type=application/json; charset=utf-8'); 
$con = mysql_connect("***","***","***"); 

if (!$con) 
    { 
    die('Could not Connect:' . mysql_error()); 
    } 

mysql_select_db("database_name",$con); 

$result = mysql_query("SELECT * FROM database_name"); 

while($row = mysql_fetch_assoc($result)) 
    { 
     $output[]=$row; 
    } 

print(json_encode($output)); 

mysql_close($con); 

?> 

PHP的迴應是:

[{ 「名稱」: 「Izzo32」, 「ID」: 「5554」, 「電子郵件」:「XXX @ hotmail.com「}}

+0

什麼是第72行? – Ahmad

+0

怎麼樣? – Izzo32

回答

0

如果你想解析JSON,那麼你應該提供JSON,而不是html。顯然你收到「< html>」標籤!

如果我是你,我會修復我的webservice並使用gson將json轉換爲java對象!

+0

謝謝,我會試試:) – Izzo32

+0

我已經使用了Gson,但同樣的錯誤發生! :/ – Izzo32

+0

你的webservice響應是什麼?你可以把它粘貼在這裏嗎? –

0

看起來像你試圖把一個非JSON格式的字符串變成一個JSON對象多數民衆贊成什麼導致你的錯誤。你應該打印出html響應來查看整個事件並在這裏​​發佈。

+0

我編輯了這個問題,請檢查它。 – Izzo32

+0

但是你想要變成一個json對象的字符串是什麼。也許它裏面的HTML括號 –

+0

對不起,我沒有得到! :/ – Izzo32

0

再次審視這個問題,我很確定我的專業知識沒有什麼用處。抱歉。

+0

沒關係,謝謝@dkgeld – Izzo32