我想从Android应用程序到我的电脑中的本地主机sava数据。 我写了一个edittext来获取我的文字:
<EditText
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
并编写一个方法将数据发送到localhost:
public void send(View v3)
{
String msg = edittext2.getText().toString();
// make sure the fields are not empty
if (msg.length()>0)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://localhost/datalog.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
edittext2.setText(""); // clear text box
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
// Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
并通过sendbutton调用方法:
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v3) {
// TODO Auto-generated method stub
// send datasend=new send();
//datasend.execute();
send(v3);
}
});
但要按摩: 08-10 01:14:26.171:V / InputMethodManager(20557):START INPUT:android.widget.EditText {41812b10 VFED..CL .F .... ID 0,672-225,731#7f080013 app:id / text2} ic = com.android.internal.widget.EditableInputConnection@418662b0 tba=android.view.inputmethod.EditorInfo@41866268 controlFlags =#100
我在服务器端写了一个php代码,获取post变量。
答案 0 :(得分:0)
我希望你在主gui线程之外的一个线程中执行Http请求。
你可以尝试这段代码:
<强> MainActivity:强>
public class MainActivity extends Activity {
private final static String TAG = MainActivity.class.getSimpleName();
EditText edittext2;
Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext2 = (EditText) findViewById(R.id.text2);
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
send();
}
});
}
public void send() {
String msg = edittext2.getText().toString();
// make sure the fields are not empty
if (!msg.equals(""))
{
Log.d(TAG, "send: " + msg);
// HttpClient httpclient = new DefaultHttpClient();
// HttpPost httppost = new HttpPost("https://localhost/datalog.php");
//
// try {
// List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// nameValuePairs.add(new BasicNameValuePair("id", "12345"));
// nameValuePairs.add(new BasicNameValuePair("message", msg));
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// httpclient.execute(httppost);
// edittext2.setText(""); // clear text box
// } catch (ClientProtocolException e) {
// // TODO Auto-generated catch block
// } catch (IOException e) {
// // TODO Auto-generated catch block
// // Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
// }
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
}
<强> activity_main.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"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/sendButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="send"
android:layout_below="@id/text2"/>
</RelativeLayout>
这对我来说效果很好,我感兴趣的是你的相同之处
获取更多信息后进行修改:
我现在用我的电脑上的简单app和另一个php文件测试了它。 该应用程序是连接到php文件,它创建一个消息文件。可以通过浏览器访问此消息文件。
Http部分必须以我这样做的任务运行:
<强> MainActivity:强>
public class MainActivity extends Activity {
private final static String TAG = MainActivity.class.getSimpleName();
EditText edittext2;
Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext2 = (EditText) findViewById(R.id.text2);
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyHttpTask mTask = new MyHttpTask();
mTask.execute("");
}
});
}
class MyHttpTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
send();
return "";
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG, "onPostExecute" + result);
edittext2.setText(result);
}
}
public void send() {
String msg = edittext2.getText().toString();
// make sure the fields are not empty
if (!msg.equals(""))
{
Log.d(TAG, "send: " + msg);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.178.60/datalog.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
//edittext2.setText(""); // clear text box
} catch (ClientProtocolException e) {
Log.d(TAG, e.toString());
} catch (IOException e) {
Log.d(TAG, e.toString());
}
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
}
确保它的NOT localhost不能连接,除了Android设备运行带有php的http服务器。
php file datalog.php:
<?php
$message=$_POST['message'];
$filename="androidmessages.html";
file_put_contents($filename,$message."<br />",FILE_APPEND);
$androidmessages=file_get_contents($filename);
echo $androidmessages;
?>
我还得到一个名为&#34; androidmessages.html&#34;的文件。用文本我键入edittext 对我来说这很好,没有任何错误
这项工作的意义在于拥有一个记录来自Android设备的消息的网页。因此,可以通过网络连接从任何浏览器查看消息