我是Android的新手,并试图从我的Android应用程序发布数据到localhost。虽然它没有显示任何错误,但它只是在Android代码中显示0<br>0
。但是在PHP代码中,它显示了"empty"
我的PHP代码是:
<?php
if (empty($_POST['name']) || empty($_POST['pass']))
{
echo "empty";
}
else{
$name = $_POST['name'];
$pass = $_POST['pass'];
echo "Name:" + $name;
echo "<br>";
echo "Pass:" + $pass;
}
?>
我只是从Android应用程序发送用户名和密码以在PHP页面中打印。我的Android代码是:
package com.example.connecttophp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText usn = (EditText)findViewById(R.id.editText1);
final EditText pwd = (EditText)findViewById(R.id.editText2);
final TextView logme = (TextView)findViewById(R.id.textView3);
Button clear = (Button)findViewById(R.id.button2);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
usn.setText("");
pwd.setText("");
logme.setText("");
}
});
Button send = (Button)findViewById(R.id.button1);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new SendDatatoPhp(logme).execute(usn.getText().toString(),pwd.getText().toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
class SendDatatoPhp extends AsyncTask<String,String,String>{
TextView logme;
public SendDatatoPhp(TextView et){
logme = et;
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/demo.php");
String responseBody;
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name",arg0[0]));
nameValuePairs.add(new BasicNameValuePair("pass",arg0[1]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
responseBody = EntityUtils.toString(response.getEntity());
return responseBody;
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
}
catch (IOException e) {
// TODO Auto-generated catch block
}
return "Login Not Successful";
}
protected void onPostExecute(String response){
logme.setText(response);
}
}
我的XML如下
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="28dp"
android:text="Enter Username:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10" >
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="36dp"
android:text="Enter Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/textView2"
android:layout_marginTop="33dp"
android:ems="10"
android:inputType="textPassword" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="37dp"
android:text="Send" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText1"
android:layout_marginRight="28dp"
android:text="Clear" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/button1"
android:layout_marginTop="62dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
我已在mainfest文件中声明了权限。
我不知道错误是什么。
我希望PHP文件能够接收用户名和密码并打印出来。
我该怎么做?
答案 0 :(得分:0)
检查此行后是否有值。
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
Log.e("uname",arg[0]);
Log.e("pass",arg[1]);
把日志和检查你有没有价值..?