Java android HTTP post php脚本问题
android http将数据从android发送到web我制作了一个将数据从android发送到web的应用程序android应用程序send data perfact,但我不知道如何用php编写代码来接收来自android的数据。 我正在使用xampp服务器编写php脚本。这是mt应用程序代码Java android HTTP post php脚本问题,java,php,android,http,Java,Php,Android,Http,android http将数据从android发送到web我制作了一个将数据从android发送到web的应用程序android应用程序send data perfact,但我不知道如何用php编写代码来接收来自android的数据。 我正在使用xampp服务器编写php脚本。这是mt应用程序代码 public class Main extends Activity implements OnClickListener{ private EditText value;
public class Main extends Activity implements OnClickListener{
private EditText value;
private Button btn;
private ProgressBar pb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
value=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(value.getText().toString().length()<1){
// out of range
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(value.getText().toString());
}
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0]);
return null;
}
protected void onPostExecute(Double result){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}
public void postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://somewebsite.com/receiver.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
public类主扩展活动实现OnClickListener{
私有文本值;
专用按钮btn;
私人ProgressBar pb;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、主布局);
值=(EditText)findViewById(R.id.editText1);
btn=(按钮)findViewById(R.id.button1);
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(此);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.home,menu);
返回true;
}
公共void onClick(视图v){
//TODO自动生成的方法存根
如果(value.getText().toString().length(),请按照以下步骤操作
创建名为test.php的文件,并将php代码复制到该文件中
将该文件复制到xamp服务器根文件夹(在侧WWW目录中)
确保您的手机和计算机连接到同一个WiFi路由器(如果是),然后复制系统的IP地址
复制的IP地址将显示在您的服务器IP上。您可能需要搜索端口号
经过以上4个步骤后,您将得到如下URL
地址:端口号/test.php
范例
运行xamp服务器
在android http请求中设置此url
在谷歌上查找错误
按照步骤操作
创建名为test.php的文件,并将php代码复制到该文件中
将该文件复制到xamp服务器根文件夹(在侧WWW目录中)
确保您的手机和计算机连接到同一个WiFi路由器(如果是),然后复制系统的IP地址
复制的IP地址将显示在您的服务器IP上。您可能需要搜索端口号
经过以上4个步骤后,您将得到如下URL
地址:端口号/test.php
范例
运行xamp服务器
在android http请求中设置此url
在谷歌上查找错误
但是当我打开php文件时,它给我错误,我发布了php脚本错误的屏幕截图,请注意。很明显,它会。在浏览器中点击URL将不包含post参数。所以它的抛出错误。请检查一些基本的php编码,但当我打开php文件时,它给我错误,我发布了php脚本错误的屏幕截图,请注意。很明显,它会。Hitt在浏览器中设置URL将不包含post参数。因此其抛出错误。请检查一些基本的php编码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Enter Something Below:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:ems="10"
android:hint=""
>
<requestFocus />
</EditText>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/progressBar1"
android:layout_marginTop="24dp"
android:text="Submit" />
</RelativeLayout>
<?php
// receive data from app's http request
$data=$_POST["myHttpData"];
// write data from my android app to a text file
file_put_contents('myTextFile.txt',$data);
?>