使用API 23.不能提供LogCat(我沒有一個模擬器,我只能通過生成apk來測試)。 我一直在嘗試這些一整天,我仍然卡住。 Android應用>發送一些字符串到位於我的本地主機(使用xampp)的PHP>取回這些字符串並在TextView中顯示。PHP不發回回/解決
現在工作,我終於搞定了,主要問題是從edittext中檢索文本時,它沒有正常加載。
public class MainActivity extends AppCompatActivity {
Button b1;
TextView t1;
EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText)findViewById(R.id.editText);
t1 = (TextView)findViewById(R.id.textView);
b1 = (Button) findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
GetText();
} catch (Exception ex) {
t1.setText("url exception");
}
}
});
}
public void GetText() throws UnsupportedEncodingException {
String nombre = e1.getText().toString();
new SignIn().execute(nombre);
}
class SignIn extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... params) {
String link="http://localhost:8012/kb_test/ejemplo.php"; // localhost = your actual ip (ipconfig at cmd) i use custom port 8012, use your own.
String code = params[0];
StringBuilder sb = new StringBuilder();
try {
String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(code, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader (conn.getInputStream()));
String line = null;
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
// return sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
String aux = e.toString();
return aux;
} catch (MalformedURLException e) {
e.printStackTrace();
String aux = e.toString();
return aux;
} catch (UnknownHostException e) {
e.printStackTrace();
String aux = e.toString();
return aux;
} catch (IOException e) {
e.printStackTrace();
String aux = e.toString();
return aux;
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
return sb.toString();
}
@Override
protected void onPostExecute(String result){
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG);
t1.setText(result);
}
}
的.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="141dp" />
.PHP
<?php error_reporting(E_ALL);
if (isset($_POST['name'])) {
$name = urldecode($_POST['name']);
echo 'Name: '.$name; } else { echo ' var error';}?>
**我知道我必須進入我的實際工作在IP本地主機。 **添加了Internet權限。 感謝任何需要時間幫助的人。
本地主機是我猜測不是你的服務器所在的Android設備? – RobVoisey
也許http://stackoverflow.com/a/5806384/1240523? – RobVoisey
你爲什麼要對$ _POST中的值進行urldecoding? DId你有一個額外的urlencode的內容? http://stackoverflow.com/questions/35516824/does-php-automatically-do-urldecode-on-post –