duanlaofu4108 2015-04-03 21:31
浏览 64
已采纳

Android String发布到PHP服务器

Summary -I take text from edittext and send it to server. And server put these strings to text file -There is no visible error. -Server gets my post but it has onyl null string.

Here is my post code

 public void onClick(View v) {
 new AlertDialog.Builder(GorusHataBildirimActivity.this)
  .setTitle("Bildirim Yola")
    .setMessage("Bildirim yollamak istiyor musunuz ?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
Thread thread = new Thread(new Runnable(){
     @Override
     public void run() {
          try {
EditText isim = (EditText) findViewById(R.id.editText);
EditText email = (EditText) findViewById(R.id.editText2);
EditText mesaj = (EditText) findViewById(R.id.editText3);
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox);
CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
String a = isim.getText().toString();
String b = email.getText().toString();
String c = mesaj.getText().toString();
String s = "Debug-infos:";
String k = "Debug-infos:";
if (checkBox1.isChecked()) {

s += "
 OS Version: " + System.getProperty("os.version") + "(" + android.os.Build.VERSION.INCREMENTAL + ")" + "
 OS API Level: " + android.os.Build.VERSION.SDK_INT;
}else {
s= "0";
}

if (checkBox2.isChecked()) {

k += "
 Device: " + android.os.Build.DEVICE + "
 Model (and Product): " + android.os.Build.MODEL + " (" + android.os.Build.PRODUCT + ")";
}else {
k = "0";
}
HttpClient client=new DefaultHttpClient();
HttpPost getMethod=new HttpPost("http: my  url /bildirimler/bildirim.php");
try{


List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("isim","dsa"));
nameValuePairs.add(new BasicNameValuePair("email",b));
nameValuePairs.add(new BasicNameValuePair("mesaj",c));
nameValuePairs.add(new BasicNameValuePair("d1",s));
nameValuePairs.add(new BasicNameValuePair("d2",k));
getMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
client.execute(getMethod);


} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
          } catch (Exception e) {
e.printStackTrace();
          }
     }
});

thread.start();

i think this code (android side ) does not work correctly.

here is php code

<?php

$dateFile = date('d-m-Y_hia').".txt";

$dataString = "isim:" . $isim . "mail:" . $mail.  "mesaj:" . $mesaj.  "d1:" . $d1. "d2:" . $d2.  "
";
$fWrite = fopen($dateFile,"a");
$wrote = fwrite($fWrite, $dataString);
fclose($fWrite);
print "file created and written to";

?>

Moreover ,
nameValuePairs.add(new BasicNameValuePair("isim","dsa"));
in there i used "dsa" instead of using string name like a b c ( my strings in my code ) but stil doesnt work.

my output file is like "isim:mail:mesaj:d1:d2:"

  • 写回答

1条回答 默认 最新

  • dongwu9170 2015-04-03 21:45
    关注

    Looks like you need to properly capture the data in the PHP code.

    <?php
    
    if (isset($_POST['isim']) && isset($_POST['email']) && isset($_POST['mesaj']) && isset($_POST['d1']) && isset($_POST['d2'])) {
    
     $isim = $_POST['isim'];
     $mail = $_POST['email'];
     $mesaj = $_POST['mesaj'];
     $d1 = $_POST['d1'];
     $d2 = $_POST['d2'];
    
     $dateFile = date('d-m-Y_hia').".txt";
    
     $dataString = "isim:" . $isim . "mail:" . $mail.  "mesaj:" . $mesaj.  "d1:" . $d1. "d2:" . $d2.  "
    ";
     $fWrite = fopen($dateFile,"a");
     $wrote = fwrite($fWrite, $dataString);
     fclose($fWrite);
     print "file created and written to";
    } else {
     print "missing required input";
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题