所以我试图将我的Android应用程序连接到在wamp上托管的apache服务器上的数据库,但我目前没有运气。我一直拒绝与http://10.0.2.2拒绝的联系'错误。我在androidmanifest中有必要的权限连接到互联网,我已经尝试将端口80添加到ip url但没有快乐。它一直在崩溃的地方是尝试获取httpResponse。
public class task extends AsyncTask<Void, Void, Boolean>
{
private final String mEmail;
private final String mPassword;
task(String email, String password) {
mEmail = email;
mPassword = password;
}
private ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
InputStream is = null ;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Checking credentials...");
progressDialog.show();
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
task.this.cancel(true);
}
});
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
try {
// Simulate network access.
Thread.sleep(2000);
} catch (InterruptedException e) {
return false;
}
String url_select = "http://10.0.2.2/demo.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
//This is where it crashes
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
//read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
//Toast.makeText(MainActivity.this, "Please Try Again", Toast.LENGTH_LONG).show();
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error converting result "+e.toString());
}
for (String credential : DUMMY_CREDENTIALS) {
String[] pieces = credential.split(":");
if (pieces[0].equals(mEmail)) {
// Account exists, return true if the password matches.
return pieces[1].equals(mPassword);
}
}
// TODO: register the new account here.
return true;
}
protected void onPostExecute(Void v) {
try {
JSONArray Jarray = new JSONArray(result);
for(int i=0;i<Jarray.length();i++)
{
JSONObject Jasonobject = null;
//text_1 = (TextView)findViewById(R.id.txt1);
Jasonobject = Jarray.getJSONObject(i);
//get an output on the screen
//String id = Jasonobject.getString("id");
String name = Jasonobject.getString("email");
String password = Jasonobject.getString("password");
if(mEmailView.getText().toString().equalsIgnoreCase(name) &&
mPasswordView.getText().toString().equalsIgnoreCase(password))
{
this.progressDialog.dismiss();
String address = Jasonobject.getString("address");
int alarmStatus = Jasonobject.getInt("alarm_status");
Intent mainApp = new Intent(LoginActivity.this, MainAppActivity.class);
startActivity(mainApp);
finish();
}
//text_1.append(id+"\t\t"+name+"\t\t"+password+"\t\t"+"\n");
}
this.progressDialog.dismiss();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
错误日志
04-25 20:24:58.339 8406-8406/project.cit.ie.hmsapp W/System: ClassLoader referenced unknown path: /data/app/project.cit.ie.hmsapp-2/lib/x86
04-25 20:24:58.821 8406-8413/project.cit.ie.hmsapp W/art: Suspending all threads took: 29.019ms
04-25 20:24:58.941 8406-8435/project.cit.ie.hmsapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-25 20:24:58.946 8406-8406/project.cit.ie.hmsapp D/: HostConnection::get() New Host Connection established 0xab4ff1c0, tid 8406
04-25 20:24:59.141 8406-8435/project.cit.ie.hmsapp I/OpenGLRenderer: Initialized EGL, version 1.4
04-25 20:24:59.342 8406-8435/project.cit.ie.hmsapp W/EGL_emulation: eglSurfaceAttrib not implemented
04-25 20:24:59.342 8406-8435/project.cit.ie.hmsapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdbe9e0, error=EGL_SUCCESS
04-25 20:24:59.481 8406-8413/project.cit.ie.hmsapp W/art: Suspending all threads took: 166.545ms
04-25 20:24:59.695 8406-8406/project.cit.ie.hmsapp I/Choreographer: Skipped 41 frames! The application may be doing too much work on its main thread.
04-25 20:25:00.437 8406-8406/project.cit.ie.hmsapp I/Choreographer: Skipped 44 frames! The application may be doing too much work on its main thread.
04-25 20:27:35.328 8406-8435/project.cit.ie.hmsapp W/EGL_emulation: eglSurfaceAttrib not implemented
04-25 20:27:35.328 8406-8435/project.cit.ie.hmsapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdbfd40, error=EGL_SUCCESS
04-25 20:27:37.486 8406-10704/project.cit.ie.hmsapp E/log_tag: Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2 refused
04-25 20:27:37.486 8406-10704/project.cit.ie.hmsapp E/log_tag: Error converting result java.lang.NullPointerException: lock == null
04-25 20:27:37.612 8406-8413/project.cit.ie.hmsapp W/art: Suspending all threads took: 13.023ms
04-25 20:27:37.835 8406-8417/project.cit.ie.hmsapp I/art: Background sticky concurrent mark sweep GC freed 18903(1023KB) AllocSpace objects, 0(0B) LOS objects, 54% free, 1740KB/3MB, paused 13.974ms total 543.099ms
任何帮助都表示赞赏,一直试图绕过这个问题好几天但没有用。
注意很多这是借来的代码,我不确定它的作用,但我对它的作用和内容有足够的了解。