2015-04-03 154 views
1

摘要 -I從edittext獲取文本並將其發送到服務器。並且服務器將這些字符串放到文本文件 - 沒有可見的錯誤。 - 服務器獲取我的帖子,但它有onyl空字符串。Android String發佈到PHP服務器

這是帖子的代碼

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 += "\n OS Version: " + System.getProperty("os.version") + "(" + android.os.Build.VERSION.INCREMENTAL + ")" + "\n OS API Level: " + android.os.Build.VERSION.SDK_INT; 
}else { 
s= "0"; 
} 

if (checkBox2.isChecked()) { 

k += "\n Device: " + android.os.Build.DEVICE + "\n 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(); 

我覺得這個代碼(機器人側)工作不正常。

這裏是php代碼

<?php 

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

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

?> 

此外,
nameValuePairs.add(新BasicNameValuePair( 「ISIM」, 「DSA」));
在那裏我用「dsa」而不是像bc(我的代碼中的字符串)那樣使用字符串名,但是stil不起作用。

我的輸出文件就像 「ISIM:郵箱:mesaj:D1:D2:」

回答

0

看起來你需要正確採集數據的PHP代碼。

<?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. "\n"; 
$fWrite = fopen($dateFile,"a"); 
$wrote = fwrite($fWrite, $dataString); 
fclose($fWrite); 
print "file created and written to"; 
} else { 
print "missing required input"; 
} 

?> 
+1

非常感謝。它立即工作。 – 2015-04-03 22:00:04

相關問題