2013-04-10 86 views
0

下面的代碼未接收到PHP文件,該文件是「羽毛球協會」從這段代碼的PHP文件的輸出:的AsyncTask HTTP錯誤

print(json_encode($row['Description'])); 

這將是巨大的,如果有人可以當場錯誤。 「結果」和「是」都是空變量:

public class BackgroundAsyncTask extends AsyncTask<Void, String, Void> { 

    protected Void doInBackground(Void... params) { 
    //http post 
    try{ 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet get = new HttpGet("http://ntusocities.host22.com/post.php"); 
     HttpResponse response = client.execute(get); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    } catch(Exception e){ 
     Log.e("log_tag", "Error in http connection "+e.toString()); 
    } 
    //convert response to string 
    try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
     } 

     is.close(); 

     result=sb.toString(); 
    } catch(Exception e){ 
     Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

    //parse json data 
    try { 
     JSONObject userObject = new JSONObject(result); 
     info = userObject.getString("Description"); 
    } 
    catch(Exception ex){ 
    } 
    return null; 
    } 
} 

非常感謝!

+1

你準確得到了什麼錯誤? – 2013-04-10 17:15:45

+0

檢查您的PHP腳本提供的響應,以確保數據正在返回。嘗試在瀏覽器中運行腳本以檢查數據是否正在傳遞。 – kabuto178 2013-04-10 17:17:52

+0

@ kabuto178我在瀏覽器中運行了PHP,得到的結果是它不會記錄在應用程序中 – Ryaller 2013-04-10 17:21:05

回答

0

ü沒有在AndroidManifest.xml

<manifest xlmns:android...> 
... 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
</manifest> 

你提到的鏈接, 權限添加給數據

{"info":[{"SocietyID":"SOC003","Name":"Badminton","Type":"Sport","President":"Me","VicePresident":"You","ContactEmail":"[email protected]","Description":"badminton society"}]}<!-- some data--> 

Just remove, '[' and ']' and "<!-- some data-->" from your response. I think the "some data part is comment. 

一切都很好。

那就試試這個代碼來獲取值,

JSONObject userObject; 
try { 
userObject = new JSONObject(content); 
JSONObject info = userObject.getJSONObject("info"); 
String name = info.getString("Name"); 
String type = info.getString("Type"); 
String description = info.getString("Description"); 
    // like this fetch all value/fields 

Log.i("JOSN", name +", "+ type +", "+ description); 
} catch (JSONException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 

所有最優秀的!

+0

是的,我已經完成了 – Ryaller 2013-04-10 17:19:22

+0

嘗試一次............ String url =「http://ntusocities.host22.com/post.php」; URLConnection conexion = url.openConnection(); conexion.connect(); InputStream input = new BufferedInputStream(url.openStream()); //用BufferedReader讀取它 BufferedReader br = new BufferedReader(new InputStreamReader(input)); StringBuilder sb = new StringBuilder(); 絃線; ((line = br.readLine())!= null){ sb.append(line); } \t \t br.close(); input.close(); – surender8388 2013-04-10 17:49:28

+0

非常感謝,但它給我錯誤的線'URLConnection conexion = url.openConnection();'和 'InputStream input = new BufferedInputStream(url.openStream());' – Ryaller 2013-04-11 06:54:10

1

您的網址http://ntusocities.host22.com/post.php返回badminton society字符串。這不是有效的JSON,所以JSON解析失敗並不令人意外。您需要修復服務器端以返回有效的JSON。

什麼是JSON? - >http://en.wikipedia.org/wiki/JSON

如何檢查JSON是否有效? - >http://jsonlint.com/

而且,有一個非常簡單的方式來獲得響應的字符串:https://stackoverflow.com/a/4480517/247013

+0

那麼我如何使其有效JSON? – Ryaller 2013-04-10 20:27:03

+0

它代表什麼數據結構?這是一個關鍵值對嗎?或者它是一個數組? – 2013-04-10 23:55:23

+0

我想要發生一個MySQL查詢的結果,所以行的其餘部分將發送回單獨的變量 – Ryaller 2013-04-11 06:48:44

0

很好,從URL輸出是:

"badminton society" 
<!-- Hosting24 Analytics Code --> 
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script> 
<!-- End Of Analytics Code --> 

意味着它不是JSON,像Arhimend已經提到過。但是這不應該阻止你獲得有效的InputStream。試試這個:

protected Void doInBackground(Void... params) { 
//http post 
try{ 
    is = new java.net.URL("http://ntusocities.host22.com/post.php").openStream(); 
} catch(Exception e){ 
    Log.e("log_tag", "Error in http connection ", e); 
} 
... 

不管怎樣,代碼將無法在

JSONObject userObject = new JSONObject(result); 

解析,因爲 「結果」 是不是JSON。