黑马程序员技术交流社区
标题:
java 通过httpurlconnect来访问lnmp服务器传递和获取字符串。。...
[打印本页]
作者:
nuoxi0318
时间:
2013-7-25 18:29
标题:
java 通过httpurlconnect来访问lnmp服务器传递和获取字符串。。...
本帖最后由 杨兴庭 于 2013-7-28 22:53 编辑
package com.example.test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.example.test1.R;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity{
String urlString = "http://127.0.0.1:8888/test/hello.php";
private URL url;
String line;
String result = "";
private HttpURLConnection httpUrlconnection;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickListener lis=new OnClickListener(){
@SuppressLint("ShowToast")
public void onClick(View v)
{
try {
url = new URL(urlString);
httpUrlconnection = (HttpURLConnection) url.openConnection();
httpUrlconnection.setRequestMethod("POST");
httpUrlconnection.setDoOutput(true);
httpUrlconnection.setDoInput(true);
httpUrlconnection.connect();
PrintWriter out = new PrintWriter(httpUrlconnection.getOutputStream());
out.print(R.id.EdiUser);
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlconnection.getInputStream()));
while((line = in.readLine()) != null)
{
result += "\n"+line;
}
Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_LONG);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
Button b=(Button)findViewById(R.id.btnSure);
b.setOnClickListener(lis);
}
}
复制代码
作者:
李永康
时间:
2013-7-26 07:45
在android里面,你的127.0.0.1,指的是模拟器,而不是服务器地址。你使用ipconfig,查找到你自己的Ip,换成本地ip
作者:
李永康
时间:
2013-7-26 07:46
或者使用 10.xx。xx。xx什么的,具体忘了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2