我已经成功地将代码连接到了计算机的php文件,并在Java程序中输出了正确的文本。但是,当我尝试将它添加到我的Android项目中以显示高分时,总是会抛出一个IOException错误,我无法找出原因。以下是我的代码。如果您能提供任何帮助,我将不胜感激。
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("它起作用了!"); } catch(MalformedURLException e) { HscoreText.setText("URL格式错误"); } catch (IOException e) { HscoreText.setText("输入/输出错误"); }
}
}