我刚开始用php连接android应用程序 当我在本地wamp服务器上运行时,我的文件工作正常 在远程服务器上传后,我得到了我想要的东西,但我在我的模拟器上得到了一些额外的代码。我想摆脱它 请帮忙。 当我使用html文件在我的chrome中调用相同的文件时,它也可以正常工作。
这是我的Android应用程序的代码
public class LoginActivity extends Activity {
HttpClient hc;
HttpPost hp;
List<NameValuePair> nvp;
HttpResponse hr;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText username=(EditText) findViewById(R.id.username);
final EditText password=(EditText) findViewById(R.id.password);
Button b=(Button) findViewById(R.id.register);
final TextView res=(TextView) findViewById(R.id.textView3);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// String result="Username: "+username.getText().toString()+"\n"+"Password: "+password.getText().toString();
//res.setText(result);
try
{
hc=new DefaultHttpClient();
hp=new HttpPost("http://pratik30.host56.com/insertquery.php");
nvp=new ArrayList<NameValuePair>(2);
nvp.add(newBasicNameValuePair("username", username.getText().toString()));
nvp.add(new BasicNameValuePair("password", password.getText().toString()));
hp.setEntity(new UrlEncodedFormEntity(nvp));
//hr=hc.execute(hp);
ResponseHandler<String> rs= new BasicResponseHandler();
String r=hc.execute(hp,rs);
res.setText(r);
}
catch(Exception e)
{
res.setText(e.getLocalizedMessage());
}
}
});
}
}
这是php文件
<?php
$dbc=mysqli_connect('localhost','root','mypassword','pratik') or die('Error connecting to mysql server');
$user=$_POST['username'];
$pass=$_POST['password'];
echo $user;
echo $pass;
$query = "Insert into users values ('$user','$pass');";
$result=mysqli_query($dbc,$query) or die('Error querying database');
mysqli_close($dbc);
?>
Here is a picture我得到的输出。
答案 0 :(得分:0)
你的php返回以下内容你必须从那里删除它:
<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->
您将整个结果文本设置为您的
TextView res.setText(r);