android发送字符串,java – 如何从Android发送字符串到PHP

博主在尝试创建一个在线应用程序时遇到问题,无法从Android应用向PHP脚本发送字符串。在MainActivity.java中,已经设置了HttpPost请求并将编辑框输入的字符串作为参数传递,但在PHP端接收到的数据为NULL。PHP代码尝试从POST变量中获取用户名,但未能成功。问题可能出在Android端的请求处理或PHP端的数据接收上。
摘要由CSDN通过智能技术生成

我试图创建一个在线应用程序.我想从我的应用程序发送一个字符串到我的

PHP脚本,但它结果是php没有从我的应用程序接收任何字符串,这意味着PhP将回显NULL.我一直在网上进行研究并寻找解决方案,但都没有奏效.

下面是我的MainActivity.java代码:

package my.com.tutionathome.calvinlau.testserver;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;

import org.apache.http.client.ResponseHandler;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.BasicResponseHandler;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;

import android.app.ProgressDialog;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity {

Button b;

EditText et;

TextView tv;

HttpPost httppost;

StringBuffer buffer;

HttpResponse response;

HttpClient httpclient;

List nameValuePairs;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

b = (Button)findViewById(R.id.Button01);

et= (EditText)findViewById(R.id.EditText01);

tv= (TextView)findViewById(R.id.tv);

b.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

final ProgressDialog p = new ProgressDialog(v.getContext()).show(v.getContext(),"Waiting for Server", "Accessing Server");

Thread thread = new Thread()

{

@Override

public void run() {

try{

httpclient=new DefaultHttpClient();

httppost= new HttpPost("http://10.0.0.2/my_folder_inside_htdocs/connection.php"); // make sure the url is correct.

//add your data

nameValuePairs = new ArrayList(1);

// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,

nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $"username" = $_POST['username'];

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

//Execute HTTP Post Request

response=httpclient.execute(httppost);

ResponseHandler responseHandler = new BasicResponseHandler();

final String response = httpclient.execute(httppost, responseHandler);

System.out.println("Response : " + response);

runOnUiThread(new Runnable() {

public void run() {

p.dismiss();

tv.setText("Response from PHP : " + response);

}

});

}catch(Exception e){

runOnUiThread(new Runnable() {

public void run() {

p.dismiss();

}

});

System.out.println("Exception : " + e.getMessage());

}

}

};

thread.start();

}

});

}

}

我的PHP代码:

// put your code here

$hostname_localhost ="localhost";

$database_localhost ="android";

$username_localhost ="root";

$password_localhost ="";

$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)

or

trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$username = $_POST['username'];

$password = $_POST['password'];

$query_search = "select Email, Username from tblmember where Username = '".$username."' AND Email = '".$password. "'";

$query_exec = mysql_query($query_search) or die(mysql_error());

$rows = mysql_num_rows($query_exec);

if($username == NULL){

echo "NULL";

}else{

echo $username;

}

?>

这是我的Android Manifest:

package="my.com.tutionathome.calvinlau.testserver">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值