gpt4 book ai didi

java - 如何将字符串从Android发送到php

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:40 25 4
gpt4 key购买 nike

我正在尝试创建一个在线应用程序。我想从我的应用程序向我的 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<NameValuePair> 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<NameValuePair>(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<String> 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 代码:

<?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 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.com.tutionathome.calvinlau.testserver">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

最佳答案

有很多库可以促进这一点,节省很多代码行,我特别建议 ​​retrofit .

关于java - 如何将字符串从Android发送到php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38236743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com