我每天都在學習更多關於android開發和json代碼的知識。 但現在我被困在這個; 我可以從我的在線數據庫中獲取我的值並顯示它,但是我看到了整個json代碼。 我希望看到我希望它展示的部分。android:創建json對象或數組?
這是我的代碼,我認爲這是很基本的,但我也學習:)
正如你可以看到我正準備從網頁的價值,並把它在我的TextView,但我會喜歡把它放在一個JSONObject或JSONArray中,不知道女巫更好。
有人可以幫我一下嗎?
隨着親切的問候
public class Bordje extends Activity{
public void onCreate (Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bordje);
//This is out textview element, obtained by id from XML Layout
TextView myListView = (TextView)findViewById(R.id.netResult2);
//Lets connect to the internet
try {
String result = "";
//create new client object
HttpClient httpclient = new DefaultHttpClient();
//now post to the url
HttpPost httppost = new HttpPost("http://www.wvvzondag2.nl/android/leesbordje.php");
//execute url
HttpResponse response = httpclient.execute(httppost);
//get message from the response
HttpEntity entity = response.getEntity();
//get the content from message
InputStream webs = entity.getContent();
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
//slow our inputstream
webs.close();
//puts the resut into a string
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//Parsing the JSON Data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
json_data.getString("introtext");
//Get an output to the screen
//then here should be some code that displays text?
//myListView.setText(Html.fromHtml(json_data)); ?
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
}
catch (Exception e)
{
//this is the line of code that sends a real error message to the log
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
}
}
因此,去你的網站給你的整個數據集,你想從那裏解析它?你打算使用任何你要解析的數據嗎?如果沒有,你正在運行成不需要如果數據不應該被曝光(可能的安全風險)2)大量的帶寬需求(Web服務器正在推動更多的數據1)暴露數據的問題,並且您的用戶需要使用更多數據才能使用它)。 http://federmanscripts.com/2010/12/12/php-routing/可能是你感興趣的(不是PHP用戶,所以不確定質量,但它至少包含路由)。 – Robert