我正在尝试使用下面给出的代码使用端口号8080在wamp的apache localhost上发布数据。
public class postdata extends Activity
{
Button btnpost;
EditText txtname,txtsalary;
TextView lblstatus;
String hostname;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main3);
btnpost=(Button)this.findViewById(R.id.btnpost);
txtname=(EditText)this.findViewById(R.id.txtname);
txtsalary=(EditText)this.findViewById(R.id.txtsalary);
final HttpClient client = new DefaultHttpClient();
final HttpPost post = new HttpPost("http://127.0.0.1:8080/andy1/script2.php");
btnpost.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
try
{
List<NameValuePair> pair = new ArrayList<NameValuePair>(2);
pair.add(new BasicNameValuePair("txtname",txtname.getText().toString()));
pair.add(new BasicNameValuePair("txtsalary",txtsalary.getText().toString()));
post.setEntity(new UrlEncodedFormEntity(pair));
HttpResponse response = client.execute(post);
Toast.makeText(getApplicationContext(), "Record Saved",1000).show();
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
}
catch (IOException e)
{
Toast.makeText(getApplicationContext(), "Error in uploading",1000).show();
}
}
});
}
}
每当我在模拟器上运行上面的代码时,我都会收到以下错误。
12-03 10:10:51.237: E/AndroidRuntime(281): FATAL EXCEPTION: main
12-03 10:10:51.237: E/AndroidRuntime(281): java.lang.IllegalArgumentException: Host name may not be null
12-03 10:10:51.237: E/AndroidRuntime(281): at org.apache.http.HttpHost.<init>(HttpHost.java:83)
12-03 10:10:51.237: E/AndroidRuntime(281): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
12-03 10:10:51.237: E/AndroidRuntime(281): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-03 10:10:51.237: E/AndroidRuntime(281): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-03 10:10:51.237: E/AndroidRuntime(281): at demo.network.postdata$1.onClick(postdata.java:65)
12-03 10:10:51.237: E/AndroidRuntime(281): at android.view.View.performClick(View.java:2408)
12-03 10:10:51.237: E/AndroidRuntime(281): at android.view.View$PerformClick.run(View.java:8816)
12-03 10:10:51.237: E/AndroidRuntime(281): at android.os.Handler.handleCallback(Handler.java:587)
12-03 10:10:51.237: E/AndroidRuntime(281): at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 10:10:51.237: E/AndroidRuntime(281): at android.os.Looper.loop(Looper.java:123)
12-03 10:10:51.237: E/AndroidRuntime(281): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-03 10:10:51.237: E/AndroidRuntime(281): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 10:10:51.237: E/AndroidRuntime(281): at java.lang.reflect.Method.invoke(Method.java:521)
12-03 10:10:51.237: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-03 10:10:51.237: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-03 10:10:51.237: E/AndroidRuntime(281): at dalvik.system.NativeStart.main(Native Method)
我也尝试使用10.0.0.2作为IP地址,因为它是其他帖子中发布的解决方案之一但根本不起作用。 我已经尝试过使用net解决这个错误很多但是无法成功。
答案 0 :(得分:2)
要访问localhost(开发系统),您必须使用 10.0.2.2 (而不是10.0.0.2)。
了解更多信息,请查看here
答案 1 :(得分:1)
除非您在模拟器中运行Web服务器,否则您需要使用开发计算机的IP地址。使用ifconfig
(Linux等)或ipconfig
(Windows)查找地址。对于本地网络应该是类似'192.168.xxx.xxx`的东西。
或者使用10.0.2.2来引用开发机器的环回接口,如另一个答案中所建议的那样。以下是模拟器相关文档的链接:http://developer.android.com/tools/devices/emulator.html#networkaddresses
答案 2 :(得分:0)
你可以从互联网上找到ip ..来到http://whatsmyip.net/,你的项目中使用了这个ip
最终的HttpPost帖子=新的HttpPost(“http:// ip :8080 / andy1 / script2.php”);