【问题标题】:post data to localhost in android app在android应用程序中将数据发布到本地主机
【发布时间】:2013-07-31 05:20:11
【问题描述】:

我是 android 新手,正在尝试从我的 android 应用程序将数据发布到 localhost。虽然它没有显示任何错误,并且总是说命令已发送,但它没有写入文件,或者它可能没有发布任何数据。谁能发现问题,或者告诉我该如何解决?

MainActivity.java

package com.example.register;

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 android.app.Activity;
import android.opengl.Visibility;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

private EditText value;
private Button btn;
private ProgressBar pb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void onClick(View v) {
// TODO Auto-generated method stub
if(value.getText().toString().length()<1){
// out of range
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(value.getText().toString());  
}

}

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{

@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0]);
return null;
}

protected void onPostExecute(Double result){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}

public void postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/text.php");

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}

}
}

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" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Enter Something Below:"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:ems="10"
android:hint=""
>

<requestFocus />
</EditText>

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/progressBar1"
android:layout_marginTop="24dp"
android:text="Submit" />

</RelativeLayout>

【问题讨论】:

  • 打印响应字符串?你去那里做什么?

标签: php android localhost


【解决方案1】:

您必须使用10.0.2.2 IP 地址连接本地主机系统。

HttpPost httppost = new HttpPost("http://localhost/text.php");

应该是

HttpPost httppost = new HttpPost("http://10.0.2.2/text.php");

【讨论】:

  • 我用过这个但还是没有发帖显示命令发送
  • @RohitGoel 检查您在HttpResponse response = httpclient.execute(httppost); 中得到的回复。
  • 你能告诉我如何打印回复
  • 为我工作。谢谢你:D
【解决方案2】:
  1. 使用 IP 地址而不是 localhost。示例:

    HttpPost httppost = new HttpPost("http://192.168.0.10/mypage.php");
    
  2. AndroidManifest.xml 示例中添加互联网权限:

【讨论】:

  • 192.168.0.10 localhost 是哪一种方式?
  • @MdKamruzzaman:你离真相太远了。首先是MongoDB绑定地址的问题。其次,虽然机器的所有其他 IP 可能会更改,但 localhost 或 127.0.0.1 永远不会更改,因为它们是这样定义的。
猜你喜欢
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
  • 2019-12-05
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
相关资源
最近更新 更多