[英]Symfony 3 JSONResponse on Android
所以即時通訊使用createQuery這樣連接到我的Symfony數據庫和Doctrine
// JSON ACTION
public function jsonAction()
{
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery('SELECT pa FROM CBMedBundle:Patients pa');
$myArray = $query->getArrayResult();
return new JsonResponse($myArray);
}
在url上: http://localhost/Symfony/web/app_dev.php/json現在,我想在Android(eclipse android)上導出結果(jsonreponse)並在那里顯示響應! 我怎樣才能做到這一點 ?
我在Google上找到了有關在Android上顯示json代碼的代碼,但我不知道如何適應我的問題。 謝謝
public class MainActivity extends Activity {
TextView t;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.text1);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
t.setText(getServerData(strURL));
}
public static final String strURL = "http://10.0.2.2/connexion.php";
private String getServerData(String returnString) {
InputStream is = null;
String result ="";
ArrayList<NameValuePair> nameValuePaires = new ArrayList<NameValuePair>();
nameValuePaires.add(new BasicNameValuePair("tblville","tblville"));
// Envoie de la commande Http
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(strURL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePaires));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("log_tag","Error in http connection "+e.toString());
}
//conversion de la réponse en chaine de caractère
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-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());
}
//recuperation des donnees json
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","ID_ville : "+json_data.getString("ID_ville")+" , Nom_ville : "+json_data.getString("Nom_ville")
);
returnString += "\n\t" + jArray.getJSONObject(i);
}
}
catch(JSONException e){
Log.e("log_tag","Error Parsing data"+e.toString());
}
return returnString;
}
}
編輯:JSONresponse結果
[{"id":1,"nom":"Kane","prenom":"Samba","DateNaissance":{"date":"1995-03-08 00:00:00","timezone_type":3,"timezone":"Europe\/Paris"},"LieudeNaissance":"Nouakchott","sexe":"Homme","adresse":"Tunis","profession":"Etudiant","NumerodeTel":"+21629760962","Email":"kane_samba4@yahoo.fr","SituationFamilial":"Mari\u00e9"},{"id":2,"nom":"Bebeskho","prenom":"AllStar","DateNaissance":{"date":"1995-02-08 00:00:00","timezone_type":3,"timezone":"Europe\/Paris"},"LieudeNaissance":"Bamako","sexe":"Homme","adresse":"Tunis","profession":"Cuisinier","NumerodeTel":"+2167583486","Email":"bebeskho@gmail.com","SituationFamilial":"C\u00e9libataire"},{"id":3,"nom":"Vecenzo","prenom":"Gaimno","DateNaissance":{"date":"1995-01-01 00:00:00","timezone_type":3,"timezone":"Europe\/Paris"},"LieudeNaissance":"Tunis","sexe":"Homme","adresse":"Ariana","profession":"Etudiant","NumerodeTel":"+21654758","Email":"vecenzogo","SituationFamilial":"Mari\u00e9"},{"id":4,"nom":"Diara","prenom":"Bakary","DateNaissance":{"date":"1995-06-10 00:00:00","timezone_type":3,"timezone":"Europe\/Paris"},"LieudeNaissance":"Dakar","sexe":"Homme","adresse":"Dakar Sacr\u00e9 Coeur","profession":"Etudiant","NumerodeTel":"+2214753526","Email":"diaBakis@gmail.com","SituationFamilial":"C\u00e9libataire"}]
現在,我只想在我的Android模擬器應用程序(Eclipse)上顯示此結果
為了使代碼適合您的目的-您需要更改URL以指向您的服務器。 更改public static final String strURL = "http://10.0.2.2/connexion.php";
類似於http://<your-server-ip-address>/Symfony/web/app_dev.php/json
(我假設HTTP-GET請求返回您粘貼的JSON數組)。 然后,您修改JSON處理代碼以提取JSON文件中包含的詳細信息-如下所示:
for(int i=0;i<jArray.length();i++)
{
JSONObject personJSONData= jArray.getJSONObject(i);
String id = personJSONData.getString("id");
String nom = personJSONData.getString("nom");
//do the same for the other attributes
//note that according to your JSON file, DateNaissance is a JSONObject - so you have to do something like
JSONObject dateNaissanceObject = personJSONData.getJSONObject("DateNaissance");
}
但是,我確實建議您使用Gson之類的方法將傳入的JSON有效內容解析為例如“ Person”對象的列表。 例如,假設您已創建一個Person類,則可以執行以下操作:
List<Person> persons = gson.fromJson(result, new TypeToken<List<Person>>(){}.getType());
。 這里就是一個很好的例子-而在這里是用GSON另一個很好的例子-也有一個很好的教程在這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.