J'ai mon émulateur Android pour le moment se connecter à mon ordinateur local. Il contacte le PHP et publie une adresse IP à exécuter. Il fait ensuite écho à un résultat JSON. Je veux imprimer le résultat, mais j'ai du mal à changer l'écran android.Imprimer réponse JSON sur l'écran Android
Voici mon activité:
public class MainActivity extends Activity implements OnClickListener {
EditText ipAddress;
Button bSearch;
String IP;
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> nameValuePairs;
HttpResponse response;
HttpEntity entity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize(){
ipAddress = (EditText) findViewById(R.id.IPaddress);
bSearch = (Button) findViewById(R.id.searchBtn);
bSearch.setOnClickListener((OnClickListener) this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://127.0.0.1/myfiles/WorkingVersionVJSON.php");
IP = ipAddress.getText().toString();
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("ipaddress", IP));
try{
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
entity = response.getEntity();
if(entity != null){
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
String rIP = jsonResponse.getString("ipaddress");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
Et le PHP
<?php
require("IPQFunctionworkingversionV7.php");
$ipaddress = $_POST["ipaddress"];
$results = array();
$results = getScore($ipaddress);
echo json_encode($results);
?>
Le PHP fonctionne quand je le lance dans mon navigateur et après à l'aide html. Je suppose que la ligne
nameValuePairs.add(new BasicNameValuePair("ipaddress", IP));
Messages au php de la même manière, puisque IP doit être ce qui est entré dans la zone de texte sur l'androïde et correspond à la POST dans le PHP.
L'activité se compile et se trouve sur l'émulateur, donc je suppose que la connexion est réussie. J'ai juste besoin d'analyser la réponse JSON d'une manière que je peux l'obtenir à afficher sur l'écran. Une fois que je l'ai, je vais travailler sur la mise en page.