我正在开发一个应用程序,它应该连接到数据库并从中获取一些数字,但无论我做什么,我得到凌空超时错误,我无法解决我的问题。以下是我的代码:
android部分:
public int digicalculator()
{
//int digipoints=0;
// Log.d("ggggggggg","digicallll");
fetchPoints("hh");
digipoints = digipoints + 30;
return digipoints;
}
private void fetchPoints(String email)
{
// Tag used to cancel the request
String tag_string_req = "fetch_points";
Log.d("ggggggggg", "digicallll");
// Toast.makeText(Pointcalculator.this,"in fetch", Toast.LENGTH_LONG).show();
// pD.setMessage("Fetching Points ...");
showDialog(10);
StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.URL_FETCH_USER_POINTS, new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Toast.makeText(getApplicationContext(), "hereee", Toast.LENGTH_LONG).show();
Log.d(TAG, "onResponse");
Log.d(TAG, "FETCH RESPONSE: " + response.toString());
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
//hideDialog(10);
try {
JSONObject jObj = new JSONObject(response);
// boolean error = jObj.getBoolean("error");
// if (!error)
// {
// Now store the user in sqlite
String totalpts = (jObj.getString("totalpoints"));
String digipts = (jObj.getString("digipoints"));
Toast.makeText(getBaseContext(), "totalpoints : "+totalpts, Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "digipoints : "+digipts, Toast.LENGTH_SHORT).show();
try
{
totalpoints = Integer.parseInt(totalpts);
digipoints = Integer.parseInt(digipts);
}
catch(NumberFormatException nfe)
{
System.out.println("Could not parse " + nfe);
}
// Inserting row in users table
// db.addUser(name, email, uid, created_at);
Toast.makeText(getApplicationContext(), "Points successfully retrieved!", Toast.LENGTH_LONG).show();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.e(TAG, "Points retriving Error: " + error.toString());
// Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
//hideDialog();
}
}) {
@Override
protected Map<String, String> getParams()
{
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("email", "hh");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
这是appController:
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue()
{
if (mRequestQueue == null)
{
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
Network network = new BasicNetwork(new OkHttpStack());
mRequestQueue = new RequestQueue(new DiskBasedCache(new File(getCacheDir(), "volley")), network);
mRequestQueue.start();
}
return mRequestQueue;
}
public class OkHttpStack extends HurlStack {
private final OkUrlFactory mFactory;
public OkHttpStack() {
this(new OkHttpClient());
}
private OkHttpClient getUnsafeOkHttpClient()
{
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]
{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslSocketFactory);
okHttpClient.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public OkHttpStack(OkHttpClient client)
{
client=getUnsafeOkHttpClient();
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
mFactory = new OkUrlFactory(client);
}
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
return mFactory.open(url);
}
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
这是我的PHP代码:
<?php
$servername = "localhost";
$username = "**";
$password = "**";
$dbname = "**";
$con = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno())
{
mysqli_connect_error();
}
if (isset($_POST['email']))
{
// receiving the post params
$email = $_POST['email'];
$sql="select totalpoints , digipoints from users where email='$email'";
$result = $con->query($sql);
}
$row = $result -> fetch_assoc();
while($row)
{
$totalpoints = $row["totalpoints"];
$digipoints = $row["digipoints"];
}
print(json_encode($totalpoints, $digipoints));
mysql_close($con);
?>
这是我的android studio logcat:
01-23 07:05:51.135 13653-13678/com.example.neshat.test D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
01-23 07:05:51.142 13653-13678/com.example.neshat.test D/﹕ HostConnection::get() New Host Connection established 0xa42f1b40, tid 13678
01-23 07:05:51.152 13653-13678/com.example.neshat.test I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-23 07:05:51.180 13653-13678/com.example.neshat.test D/OpenGLRenderer﹕ Enabling debug mode 0
01-23 07:05:51.199 13653-13678/com.example.neshat.test W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-23 07:05:51.199 13653-13678/com.example.neshat.test W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3606320, error=EGL_SUCCESS
01-23 07:05:51.250 13653-13678/com.example.neshat.test W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-23 07:05:51.250 13653-13678/com.example.neshat.test W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3606340, error=EGL_SUCCESS
01-23 07:05:59.917 13653-13653/com.example.neshat.test E/this﹕ Points retriving Error: com.android.volley.TimeoutError
01-23 07:05:59.918 13653-13653/com.example.neshat.test D/Volley﹕ [1] Request.finish: 8850 ms: [ ] http://beeken.hevak.net/fetchPointsfromDB.php 0xc87d15d6 NORMAL 1