Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8216533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:09:34+00:00 2026-06-07T12:09:34+00:00

I have my Android emulator for the time being connecting to my local computer.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:09:35+00:00Added an answer on June 7, 2026 at 12:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Android 2.1 platform, I have 7 contacts in my emulator when
Actually,i have do a test with the server.c and client.c in the android emulator(ps:they
I'm learning Android development. I have a basic app running on the Gingerbread emulator,
When I launch the android emulator through eclipse for the first time, its menu
So here I have this first time app I'm working on. When I run
have tried to change the timezon in android-emulator but it doesn't work. I write
I have been building a project and testing it on the Android emulator. I
I've a problem that whenever I run my application in emulator in second time
I would like to run something through an Android emulator. Does anyone know if
I have noticed that closing the Android emulator by close button (the top right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.