android用戶端與伺服器互動,在模擬器能夠得到伺服器的響應,但是在真機得不到伺服器的響應,請問怎麼回事?程式如下:

來源:互聯網
上載者:User

標籤:android   http   io   ar   os   sp   for   資料   on   

============問題描述============


private static final String URL = " ";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.textView2);
user = (EditText) this.findViewById(R.id.editText1);
password = (EditText) this.findViewById(R.id.editText2);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 實現串連伺服器的功能
init();
}

// 用POST的方法實現
private void init() {
// TODO Auto-generated method stub
boolean flag = false; // 成功與否的標記
String user1 = user.getText().toString().trim();
String password1 = password.getText().toString().trim();
try {
HttpPost request = new HttpPost(URL);
List<NameValuePair> parms = new ArrayList<NameValuePair>();
parms.add(new BasicNameValuePair("id", user));
parms.add(new BasicNameValuePair("password",password1));

request.setEntity(new UrlEncodedFormEntity(parms,
HTTP.UTF_8));
HttpResponse response = new DefaultHttpClient()
.execute(request);
if (response.getStatusLine().getStatusCode() != 404) { // 現在已經發現了資料了
flag = Boolean.parseBoolean(EntityUtils.toString(response.getEntity()).trim());
response.getStatusLine().getStatusCode());

}

} catch (Exception e) {
// TODO: handle exception
tv.setText("WEB伺服器串連失敗……");
}
if (flag) {
tv.setText("使用者登入成功!" + flag);
} else {
tv.setText("使用者登入失敗!" + flag);
}
}
});
}

============解決方案1============


你的url呢?沒給具體ip路徑?  模擬器本來就在電腦上,可以直接存取伺服器。
你在真機上需要關閉電腦防火牆,通過具體的內網ip地址訪問。真機和服務端要處於同一區域網路

android用戶端與伺服器互動,在模擬器能夠得到伺服器的響應,但是在真機得不到伺服器的響應,請問怎麼回事?程式如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.