我正在尝试在我的android应用程序中使用php、json和mysql制作一个身份验证系统,但是我一直在尝试 connection to url refused
.
这是我第一次使用这种方法,它是基于一个教程,当然我会感谢任何其他的建议
这是我用java编写的register活动类:
public class RegisterActivity extends Activity {
private Button btn_register;
private TextView btn_login;
private EditText et_login;
private EditText et_pass, et_confirm_pass;
JSONParser jsonParser = new JSONParser();
// url to create new product
private static String url_create_user = "http://172.16.21.5/ladies_api/register.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
btn_login = (TextView) findViewById(R.id.tv_login);
btn_register = (Button) findViewById(R.id.btn_reg);
et_login = (EditText) findViewById(R.id.et_username);
et_pass = (EditText) findViewById(R.id.et_password);
et_confirm_pass = (EditText) findViewById(R.id.et_c_password);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String login = et_login.getText().toString();
String pwd = et_pass.getText().toString();
String c_pwd = et_confirm_pass.getText().toString();
/**REGISTER USER, GO TO HOME */
//new CreateNewUser().execute();
AsyncDataClass asyncRequestObject = new AsyncDataClass();
asyncRequestObject.execute(url_create_user, login, pwd);
}
});
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
}
});
}
private class AsyncDataClass extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String jsonResult = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", params[1]));
nameValuePairs.add(new BasicNameValuePair("pwd", params[2]));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
System.out.println("Returned Json object " + jsonResult.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonResult;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("Resulted Value: " + result);
if(result.equals("") || result == null){
Toast.makeText(RegisterActivity.this, "Server connection failed", Toast.LENGTH_LONG).show();
return;
}
int jsonResult = returnParsedJsonObject(result);
if(jsonResult == 0){
Toast.makeText(RegisterActivity.this, "Invalid username or password or email", Toast.LENGTH_LONG).show();
return;
}
if(jsonResult == 1){
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
}
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
e.printStackTrace();
}
return answer;
}
private int returnParsedJsonObject(String result){
JSONObject resultObject = null;
int returnedResult = 0;
try {
resultObject = new JSONObject(result);
returnedResult = resultObject.getInt("success");
} catch (JSONException e) {
e.printStackTrace();
}
return returnedResult;
}
}
}
这是jasonparser类
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
}
// return JSON String
return jObj;
}
}
这是logcat
04-29 02:04:05.803 15719-15719/hivemind.com.ladies d/viewrootimpl:viewpostimeinputstage进程指针0 04-29 02:04:05.983 15719-15719/hivemind.com.ladies d/viewrootimpl:viewpostimeinputstage进程指针1 04-29 02:04:05.983 15719-17866/hivemind.com.ladies i/system.out:thread-10038(apachehttplog):issbsettingenabled falsethread-10038(apachehttplog):isshipbuild true 04-29 02:04:05.993 15719-17866/hivemind.com.i/system.out:thread-10038(apachehttplog):getdebuglevel 0x4f4c thread-10038(apachehttplog):智能绑定设置为false thread-10038(apachehttplog):智能绑定设置为false,ship\u build为true,日志到文件为false,dbg为false,调试级别(1-低,2-中,3-高)是1 04-29 02:04:08.993 15719-17866/hivemind.com.ladies i/system.out:asynctask#5 calls detatch()04-29 02:04:08.993 15719-17866/hivemind.com.ladies w/system.err:org.apache.http.conn.httphostconnectexception:连接到http://172.16.21.5 refused 在org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator。java:248)在org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.org.apache.http.impl.conn.abstractpoolentry.open)上。java:172)在org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter。java:130)在org.apache.http.impl.client.defaultrequestdirector.executeoriginal(defaultrequestdirector。java:1337)在org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector。java:705)在org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient。java:578)在org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient。java:494)在org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient。java:472)在hivemind.com.ladies.authentication.registeractivity$asyncdataclass.doinbackground(registeractivity。java:116)在hivemind.com.ladies.authentication.registeractivity$asyncdataclass.doinbackground(registeractivity。java:90)在android.os.asynctask$2.call(asynctask。java:295)在java.util.concurrent.futuretask.run(futuretask。java:237)在android.os.asynctask$serialexecutor$1.run(asynctask。java:234)在java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor。java:1113)在java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor。java:588)在java.lang.thread.run(线程。java:818)原因:java.net.connectexception:5000ms后连接到/172.16.21.5(端口80)失败:isconnected失败:ehostunach(没有到的路由)主机)在libcore.io.iobridge.isconnected(iobridge。java:234)在libcore.io.iobridge.connecterrno(iobridge。java:171)在libcore.io.iobridge.connect(iobridge。java:122)在java.net.plainsocketimpl.connect(plainsocketimpl。java:183)在java.net.plainsocketimpl.connect(plainsocketimpl。java:452)在java.net.socket.connect(socket。java:884)在org.apache.http.conn.scheme.plainsocketfactory.connectsocket(plainsocketfactory)。java:124)在org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator。java:193) ... 另外15个原因:android.system.errnoexception:isconnected失败:libcore.io.iobridge.isconnected(iobridge)处的ehostunach(没有到主机的路由)。java:223) ... 22更多04-29 02:04:09.003 15719-15719/hivemind.com.ladies i/system.out:结果值:04-29 02:04:09.053 15719-15719/hivemind.com.ladies d/viewrootimpl:#1 mview=android.widget.linearlayout{29e5577 v.e。。。。。。一。0,0-0,0}04-29 02:04:09.133 15719-15719/hivemind.com.d/viewrootimpl:msg\u resized\u report:ci=rect(0,0-0,0)vi=rect(0,0-0,0)or=1 04-29 02:04:09.143 15719-15719/hivemind.com.ladies w/displaylistcanvas:displaylistcanvas在未绑定的rendernode上启动(没有mowningview)04-29 02:04:12.553 15719-15719/hivemind.com.ladies d/viewrootimpl:#3 mview=null
暂无答案!
目前还没有任何答案,快来回答吧!