我的httpresponse可以连接到xamp ..它无法连接到服务器!!使用此代码输出
请帮我解决..
package com.zul.app.login;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.h
ttp.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
private EditText txtUser;
private EditText txPass;
private EditText txStatus;
private Button btnLogin;
private String url ="http://192.168.42.175/loginandroid/cekuser.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtUser = (EditText) findViewById(R.id.user);
txPass = (EditText) findViewById(R.id.pass);
txStatus = (EditText) findViewById(R.id.statuslog);
btnLogin =(Button) findViewById(R.id.btlogin);
btnLogin.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
url = "http://192.168.42.175/loginandroid/cekuser.php";
url +="?user="+txtUser.getText().toString()+"&pass="+txPass.getText().toString();
getRequest(txStatus,url);
};
});
};
这是我的http回复:
public void getRequest(EditText txtResult, String Surl){
Log.d("getRequest",Surl);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(Surl);
try{
HttpResponse response = client.execute(request);
txtResult.setText(request(response));
}catch (Exception e){
txtResult.setText("Failed Conect To Server!!");
};
};
private String request(HttpResponse response) {
String result = "";
try{
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null){
str.append(line+"\n");
}
in.close();
result = str.toString();
}catch (Exception e){
result="error";
};
return result;
}
};
我已获得
的许可<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
如果我运行这个项目我没有错误但无法连接到服务器或无法访问我的localhost
我的php文件包含此链接loginandroid / cekuser.php
如果我在android中使用浏览器我可以连接到xamp,但如果我使用我的项目我不能连接
结果:
无法连接服务器
logcat:
08-11 10:50:36.231 19556-19556/com.zul.app.login D/getRequest: http://192.168.42.175/loginandroid/cekuser.php?user=&pass=
08-11 10:50:36.256 19556-19556/com.zul.app.login I/System.out: main calls detatch()