6

I got my code to connect to my computers php file and output the correct text in a java program. When I tried to add it to my android project inorder to display high scores It always throws an IOException and I can't figure out why. Here is my code. Any help would be appreciated.

package com.enophz.spacetrash;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Scores extends Activity {

 //private TextView HscoreText;

 /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scores);

        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Menu.class);
                startActivity(myIntent);
            }

        });

        TextView HscoreText = (TextView) findViewById(R.id.text);

        try
        {
         URL page = new URL("http://192.168.1.108/score.php");
         URLConnection pageconnection = page.openConnection();
         BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    pageconnection.getInputStream()));

         in.close();


            HscoreText.setText("It Worked!");
        }
        catch(MalformedURLException e)
        {
         HscoreText.setText("MalformedURL");
        }
        catch (IOException e)
        {
         HscoreText.setText("IOException");
  }

    }

}

1 Answer 1

2

I don't know what type of content you are expecting from the php page.. but following may help you to get the content from web server:

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(myUrl);

HttpResponse response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(
    response.getEntity().getContent(), "UTF-8"));

Now JSON will be single line so you can use:

String sResponse = reader.readLine();
JSONObject JResponse = new JSONObject(sResponse);

Otherwise to get the whole content as a string:

String sResponse;
StringBuilder s = new StringBuilder();

while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I switched my code to match your example. It compiles but it still throws an IOException when I run it in the emulator. My score.php page just spits out a line of JSON.
The offending line in both examples is the BufferedReader. Is using a BufferedReader not the best way to read from web connections?
Watch the error in LogCat. Check that if you provide internet connection permission in manifest. Also your computer localhost will be 10.0.2.2 in your emulator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.