I have my Android emulator for the time being connecting to my local computer. It contacts the PHP and posts an IP address to be run. It then echoes a JSON result. I want to print the result, but am having trouble getting the android screen to change.
Here’s my activity:
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();
}
}
And the PHP
<?php
require("IPQFunctionworkingversionV7.php");
$ipaddress = $_POST["ipaddress"];
$results = array();
$results = getScore($ipaddress);
echo json_encode($results);
?>
The PHP works when I run it in my browser and post to it using html. I assume that the line
nameValuePairs.add(new BasicNameValuePair("ipaddress", IP));
Posts to the php the same way, since IP should be what’s entered into the textbox on the android and it matches the POST in the PHP.
The activity compiles and is on the emulator, so I assume the connection is successful. I just need to parse the JSON response in a way that I can get it to display on the screen. Once I have that I’ll work on the layout.
Also, the code you have in the onClick really needs to be placed in an AsyncTask. OnClick is called from the UI thread. If you take too long in making that network call (which is indeterminate length), Android will post to your user that the app is unresponsive.
To get the string on the screen your layout needs to define a TextView that you can get with findViewById and calling setText on or by inflating one programmatically and adding it to the display.