我开发了一个HTTP GET方法,可以清楚地工作。Android - HTTP GET请求
public class GetMethodEx {
public String getInternetData() throws Exception{
new TrustAllManager();
new TrustAllSSLSocketFactory();
BufferedReader in = null;
String data = null;
try
{
HttpClient client = new DefaultHttpClient();
URI website = new URI("https://server.com:8443/Timesheets/ping");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
response.getStatusLine().getStatusCode();
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally{
if (in != null){
try{
in.close();
return data;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
这是我的模拟器的打印屏幕中检索从www.google.com
SCREEN SHOT OF GOOGLE.COM WORKING
以下代码是我的检索方法,以将其显示在屏幕上的响应时。
public class Home extends Activity {
TextView httpStuff;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.httpexample);
httpStuff = (TextView) findViewById(R.id.tvhttp);
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
GetMethodEx test = new GetMethodEx();
String returned = null;
try {
returned = test.getInternetData();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returned;
}
@Override
protected void onPostExecute(String result) {
httpStuff.setText(result);
}
但是,当我尝试用我自己的服务器。
“https://server:port/xwtimesheets/ping”
我有以下的屏幕
非常感谢您的先生。 –
@Akdeniz嗨!,我尝试了你的推荐,但是我收到了一些错误,我发布了一个问题。你能否告诉我我做错了什么? http://stackoverflow.com/questions/23900054/android-php-sending-a-get-request-to-php-server 谢谢 –