我使用Volley库处理POST请求。 我的代码适用于Android 5+ 但 排球问题不适用于android 4.1,4.2,4.3,4.4
使用lib com.android.volley:volley:1.1.1
我已经审查了以下解决方案
Issues with Volley caching mechanism
App stops after 4 minutes running volley request post
登录
Login
public class Login extends AppCompatActivity
{
private RequestQueue requestQueue;
private StringRequest request;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
requestQueue = Volley.newRequestQueue(Login.this);
request = new StringRequest(Request.Method.POST,"myurl" new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.getString("success").equals("true"))
{
String fname = jsonObject.getString("fname");
String lname = jsonObject.getString("lname");
} else {
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("username", username.getText().toString());
hashMap.put("password", password.getText().toString());
return hashMap;
}
};
requestQueue.add(request);
}
}
这是logcat:
05-28 17:59:19.607 8045-8045/com.per D/OpenGLRenderer: prepareDirty (0.00, 0.00, 684.00, 290.00) opaque 0 <0x61967068>
05-28 17:59:19.608 8045-8045/com.per D/OpenGLRenderer: clear (0.00, 290 - 290.00 = 0.00, 684.00 - 0.00 = 684.00, 290.00 - 0.00 = 290.00) opaque 0 <0x61967068>
05-28 17:59:19.609 8045-8045/com.per D/OpenGLRenderer: finish <0x61967068>
05-28 17:59:19.626 8045-8045/com.per D/OpenGLRenderer: prepareDirty (0.00, 0.00, 684.00, 290.00) opaque 0 <0x61967068>
05-28 17:59:19.626 8045-8045/com.per D/OpenGLRenderer: clear (0.00, 290 - 290.00 = 0.00, 684.00 - 0.00 = 684.00, 290.00 - 0.00 = 290.00) opaque 0 <0x61967068>
05-28 18:02:19.713 9515-9515/com.per V/Provider/Settings: from settings cache , name = speak_password , value = 1
05-28 18:02:33.764 9515-9515/com.per I/View: Touch down dispatch to android.support.v7.widget.AppCompatButton{41d41c90 VFED..C. ......I. 6,96-476,214 #7f090050 app:id/btn_login}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=212.52982, y[0]=42.3833, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=984831, downTime=984831, deviceId=2, source=0x1002 }
05-28 18:02:33.911 9515-9515/com.per I/View: Touch up dispatch to android.support.v7.widget.AppCompatButton{41d41c90 VFED..C. ...P..I. 6,96-476,214 #7f090050 app:id/btn_login}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=212.52982, y[0]=42.3833, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=984981, downTime=984831, deviceId=2, source=0x1002 }
05-28 18:02:33.999 9515-9597/com.per D/dalvikvm: create interp thread : stack size=128KB
05-28 18:02:33.999 9515-9597/com.per D/dalvikvm: create new thread
05-28 18:02:33.999 9515-9597/com.per D/dalvikvm: new thread created
05-28 18:02:33.999 9515-9597/com.per D/dalvikvm: update thread list
05-28 18:02:34.000 9515-10245/com.per D/dalvikvm: threadid=27: interp stack at 0x62ef1000
05-28 18:02:34.000 9515-10245/com.per D/dalvikvm: threadid=27: created from interp
05-28 18:02:34.000 9515-9597/com.per D/dalvikvm: start new thread
05-28 18:02:34.000 9515-10245/com.per D/dalvikvm: threadid=27: notify debugger
05-28 18:02:34.001 9515-10245/com.per D/dalvikvm: threadid=27 (OkHttp ConnectionPool): calling run()
05-28 18:02:34.148 9515-9515/com.per D/GraphicBuffer: create handle(0x61911078) (w:688, h:290, f:1)
05-28 18:02:34.175 9515-9515/com.per D/GraphicBuffer: create handle(0x61911108) (w:688, h:290, f:1)
05-28 18:02:34.199 9515-9515/com.per D/GraphicBuffer: create handle(0x61913990) (w:688, h:290, f:1)
05-28 18:03:03.923 9515-9515/com.per W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
05-28 18:03:09.938 9515-9515/com.per D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{41cd7a70 token=android.os.BinderProxy@41cd72f0 {com.per/com.per.Login}}
05-28 18:03:09.944 9515-9515/com.per D/ActivityThread: ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@41cd72f0
05-28 18:03:09.950 9515-9515/com.per D/ActivityThread: ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@41cd72f0
05-28 18:03:11.911 9515-9515/com.per D/dalvikvm: GC_FOR_ALLOC freed 2130K (37160), 22% free 11574K/14724K, paused 144ms, total 144ms
05-28 18:03:12.518 9515-10395/com.per D/dalvikvm: threadid=28: interp stack at 0x61a1d000
05-28 18:03:18.050 9515-9597/com.per D/libc-netbsd: getaddrinfo: per-inst.com NO result from proxy